Accepted Java solution


  • 49
    S
    public boolean isStrobogrammatic(String num) {
        Map<Character, Character> map = new HashMap<Character, Character>();
        map.put('6', '9');
        map.put('9', '6');
        map.put('0', '0');
        map.put('1', '1');
        map.put('8', '8');
       
        int l = 0, r = num.length() - 1;
        while (l <= r) {
            if (!map.containsKey(num.charAt(l))) return false;
            if (map.get(num.charAt(l)) != num.charAt(r))
                return false;
            l++;
            r--;
        }
        
        return true;
    }

  • 1
    H

    Nice but since there are only 0-9 we can use a char array to replace the map.


  • 2
    M
    public boolean isStrobogrammatic(String num) {
        HashMap map = new HashMap();
        map.put('0', '0');
        map.put('1', '1');
        map.put('6', '9');
        map.put('8', '8');
        map.put('9', '6');
        for (int i = 0; i < num.length(); i++) {
            if (!map.containsKey(num.charAt(i)) || (char)map.get(num.charAt(i)) != num.charAt(num.length() - i - 1)) {
                return false;
            }
        }
        return true;
    }
    

  • 4
    E

    Only five cases here, so using switch or conditional operation might be even faster (0ms with OJ):

    public boolean isStrobogrammatic(String num) {
        int left, right;
        left = 0;
        right = num.length()-1;
        
        while(left<=right) {
            if(!isGood(num.charAt(left), num.charAt(right)))
                return false;
                left++;
                right--;
        }
        return true;
    }
    
    public boolean isGood(char a, char b) {
        if( (a=='1'&&b=='1') || (a=='6'&&b=='9') || (a=='9'&&b=='6') || (a=='8'&&b=='8')  || (a=='0'&&b=='0') )
            return true;
        return false;
    }
    

  • 0
    F

    Same idea. But I use getOrDefault() instead of containsKey() and get().

    public class Solution {
        public boolean isStrobogrammatic(String num) {
            HashMap<Character,Character> map = new HashMap<>();
            map.put('0','0');
            map.put('1','1');
            map.put('6','9');
            map.put('8','8');
            map.put('9','6');
            char Default = 'a';
            char[] c = num.toCharArray();
            for(int i = 0;i<=c.length/2;i++){
                if(map.getOrDefault(c[i],Default) != c[c.length-1-i]) return false;
            }
            return true;
        }
    }
    

  • 0
    Y

    @szn1992
    Thanks. Since only numbers considered in the problem, we can use int[] instead of hashmap to achieve better performance.

    Here is my solution:

    public boolean isStrobogrammatic(String num) {
            int[] map = new int[10];
            map[6] = 9;
            map[9] = 6;
            map[1] = 1;
            map[0] = 0;
            map[8] = 8;
            
            char[] ca = num.toCharArray();
            int i = 0, j = ca.length - 1;
            while(i <= j) {
                int begin = ca[i] - '0';
                int end = ca[j] - '0';
                if((begin != 0 && map[begin] == 0) || map[begin] != end) {
                    return false;
                }
                i++;
                j--;
            }
            return true;
        }
    

  • 0
    M

    Replacing the map with an array. A little bit performance improvement.

    public boolean isStrobogrammatic(String num) {
            // number's pair. 0 - 0, 1 - 1, 6 - 9, 8 - 8, others -1
            char[] map = new char[]{'0', '1', 'n', 'n', 'n', 'n', '9', 'n', '8', '6'};
            
            for (int i = 0; i < num.length(); i++) {
                if (map[num.charAt(i) - '0'] != num.charAt(num.length() - 1 - i)) {
                    return false;
                }
            }
            return true;
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.