My java solution with hashtable


  • 0
    S
    public class Solution {
        public boolean isStrobogrammatic(String num) {
            char[] htable = new char[10]; //contains 0~9
            htable[0] = '0';
            htable[1] = '1';
            htable[6] = '9';
            htable[8] = '8';
            htable[9] = '6';
            int str_len = num.length();
            int front = 0, back = str_len -1;
            while(front < back) {
                    if(htable[num.charAt(front)-'0'] != num.charAt(back))
                        return false;
                    front++;
                    back--;
            }
            if(str_len % 2==1) {
                if(num.charAt(front) == '0' || num.charAt(front) == '1' || num.charAt(front) =='8') {
                    return true;
                } else {
                    return false;
                }
            }
            return true;
        }
    }
    

Log in to reply
 

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