Simplest Java Solution


  • 0
    D
    public boolean isStrobogrammatic(String num) {
            int len = num.length();
            for(int i=0;i<= len/2 ;i++)
            {
                char ch = num.charAt(i);
                if(isIllegal(ch))
                    return false;
                
                char lastChar = num.charAt(len-i-1);    
                switch (ch){
                    case '1': if (lastChar != '1') return false;
                    break;
                    case '0': if (lastChar != '0') return false;
                    break;
                    case '6': if (lastChar != '9') return false;
                    break;
                    case '8': if (lastChar != '8') return false;
                    break;
                    case '9': if (lastChar != '6') return false;
                    break;
                }
            }
            
            
            return true;
        }
        
        private boolean isIllegal(char ch){
            return (ch == '2' || ch == '3' ||ch == '4' ||ch == '5' ||ch == '7') ;
        }
    

Log in to reply
 

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