Sunday, November 22, 2015

Strobogrammatic Number

A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).
Write a function to determine if a number is strobogrammatic. The number is represented as a string.
For example, the numbers "69", "88", and "818" are all strobogrammatic.
Solution1: 逻辑有些乱, run time complexity is O(n), constant space.
public class Solution {
    public boolean isStrobogrammatic(String num) {
        if (num == null || num.length() == 0) {
            return false;
        }
        
        if (isSymmetric(num) && onlyContains(num)) {
            return true;
        }
        
        return false;
    }
    
    public boolean isSymmetric(String num) {
        int l = 0; 
        int r = num.length() - 1;
        while (l < r) {
            if (num.charAt(l) == num.charAt(r) && num.charAt(l) != '6' && num.charAt(l) != '9') {
                l++;
                r--;
            } else {
                if (num.charAt(l) == '6' && num.charAt(r) == '9' || num.charAt(l) == '9' && num.charAt(r) == '6') {
                    l++;
                    r--;
                } else {
                    return false;
                }
            }
        }
        return true;
    }
    
    public boolean onlyContains(String num) {
        HashSet set = new HashSet();
        set.add('0');
        set.add('1');
        set.add('6');
        set.add('8');
        set.add('9');
        
        if (num.equals("6") || num.equals("9")) {
            return false;
        }
        
        for (int i = 0; i < num.length(); i++) {
            if(!set.contains(num.charAt(i))) {
                return false;
            }
        }
        
        return true;
    }
}
Solution2: run time complexity is O(n), space complexity is O(n).
public class Solution {
    public boolean isStrobogrammatic(String num) {
        if (num == null || num.length() == 0) {
            return false;
        }
        
        int[] dic = {0,1,-1,-1,-1,-1,9,-1,8,6};
        // int[] dic = new int[10];
        // dic[0] = 0;
        // dic[1] = 1;
        // dic[6] = 9;
        // dic[8] = 8;
        // dic[9] = 6;
        
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < num.length(); i++) {
            int index = num.charAt(i) - '0';
            sb.insert(0, String.valueOf(dic[index]));
        }
        //sb.reverse();
        return num.equals(sb.toString());
    }
}
Solution3: run time complexity is O(n), constant space.
public class Solution {
    public boolean isStrobogrammatic(String num) {
        if (num == null || num.length() == 0) {
            return false;
        }
        
        int[] dic = new int[10];
        dic[0] = 0;
        dic[1] = 1;
        dic[6] = 9;
        dic[8] = 8;
        dic[9] = 6;
        
        int l = 0;
        int r = num.length() - 1;
        while (l <= r) {
            int indexL = num.charAt(l) - '0';
            int indexR = num.charAt(r) - '0';
            if (indexL != dic[indexR]) {
                return false;
            } else {
                l++;
                r--;
            }
        }
        return true;
    }
}

No comments:

Post a Comment