Simple Java solution


  • 0
    F

    Use map to store pairs. Then just use map.getOrDefault() to check the front and end.

    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;
        }
    }
    

Log in to reply
 

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