Java solution, beats 55%, what can be improved?


  • 0
    public class Solution {
        public boolean isStrobogrammatic(String num) {
            int start = 0;
            int end = num.length() - 1;
            while(start < end) {
                switch(num.charAt(start)) {
                    case('0'):
                    case('8'):
                    case('1'): {
                        if(num.charAt(end) != num.charAt(start))
                            return false;
                        break;
                    }
                    case('6'): {
                        if(num.charAt(end) != '9')
                            return false;
                        break;
                    }
                    case('9'): {
                        if(num.charAt(end) != '6')
                            return false;
                        break;
                    }
                    default:
                        return false;
                }
                start++;
                end--;
            }
            return !(start == end && num.charAt(start) != '8' && num.charAt(start) != '1' && num.charAt(start) != '0');
        }
    }
    

Log in to reply
 

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