Easy to understand solution in Java


  • 0
    T
    public class Solution {
        public boolean isStrobogrammatic(String num) {
            if(num== null || num.length()<1) return false;
            
            Map<Character, Character> map = new HashMap<>();
            map.put('6','9');
            map.put('9','6');
            map.put('8','8');
            map.put('0','0');
            map.put('1','1');
            
            int start = 0;
            int end = num.length()-1;
            while(start<end){
                Character a = Character.valueOf(num.charAt(start));
                Character b = Character.valueOf(num.charAt(end));
                if(a!=b){
                    if(map.get(a) != b) return false;
                }else{
                    if(map.get(a)!=b) return false;
                }
                start++;
                end--;
            }
            if(start == end){
                return map.get(num.charAt(start)) == Character.valueOf(num.charAt(start));
            }
            return true;
            
        }
    }

Log in to reply
 

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