an O(n) java solution by using String


  • 0
    C
    class Solution {
        public boolean isPalindrome(int x) {
            if (x<0){
                return false;
            }
    
            //turn int x to the StringBuilder
            StringBuilder xs = new StringBuilder(String.valueOf(x));
            //the index of the number left and right 
            int indexL = 0;
            int indexR = xs.length()-1;
    
            //continue to campare the number if the index of left didn't meet the index of right
            while (indexL<indexR){
                if (xs.charAt(indexL)!=xs.charAt(indexR)){
                    return false;
                }else{
                    indexL ++;
                    indexR --;
                }
            }
            return true;
    
    
        }
    }
    

Log in to reply
 

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