Java no extra space


  • 0
    R
    public class Solution {
        public boolean isPalindrome(int x) {
            if(x<0){
                return false;
            }
            if(x==0){
                return true;
            }
            int pow= (int) Math.log10(x), previousPow=pow+2;
            for(int start= x/((int) Math.pow(10,pow)), end=x%10;
                x>=9;
                pow= (int) Math.log10(x),start= x/((int) Math.pow(10,pow)), end=x%10){
                if(pow==previousPow-2) {
                    // then there just start and end have been cut off
                    if (start != end) {
                        return false;
                    }
                    previousPow=pow;
                    x -= end;
                    x -= start * ((int) Math.pow(10, pow));
                    x /= 10;
                    if (x == 0) {
                        return true;
                    }
                }
                else{
                    // then there have zero in the beginning, so more digits have been cut off
                    if(x%((int) Math.pow(10,previousPow-2-pow))==0){
                        x /= ((int) Math.pow(10,previousPow-2-pow));
                        previousPow=(int) Math.log10(x)+2;
                    }
                    else {
                        return false;
                    }
    
                }
            }
            
            if(previousPow-pow!=2){
                //not examine all the digit
                return false;
            }
            return true;
        }
    }

Log in to reply
 

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