Strobogrammatic Number


  • 0
    M
    class Solution {
        public boolean isStrobogrammatic(String num) {
            Map<Character, Character> map = new HashMap();
            
            map.put('6', '9');
            map.put('9', '6');
            map.put('8', '8');
            //map.put('5', '5');
            map.put('1', '1');
            map.put('0', '0');
            
            int len = num.length();
            
            for(int i = 0;i<len/2;i++) {
                char c = num.charAt(i);
                char d = num.charAt(len-i-1);
                if(map.get(c) == null) {
                    return false;
                }
                
                if(d != map.get(c)) {
                    return false;
                }
            }
            
            if(len %2 != 0) {
                int mid = len/2;
                char c = num.charAt(mid);
                if(map.get(c) == null) {
                    return false;
                }
                
                if(c != map.get(c)) {
                    return false;
                }
            }
            
            return true;
        }
    }
    

Log in to reply
 

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