My Java Solution without any extra space


  • 0
    Y
    public class Solution {
        public boolean isPalindrome(int x) {
            if(x < 0) return false;
            if(x < 10) return true;
            for(int i = 1;;i++){
                if((int)(x / Math.pow(10,i)) == 0){
                    for(int m = i-1; m >= 1;m = m-2){
                        if((int)(x / Math.pow(10,m)) == (int)(x % Math.pow(10,1))){
                            x = (int)(x % Math.pow(10,m));
                            x = (int)(x / Math.pow(10,1));
                        }else{
                             return false;
                        }
                     }
                     break;
                }
            }
            return true;
        }
    }

  • 0
    K
    Given problem says we should not use any extra space...While declaring variables in loop, what does it mean? It uses extra space or not??

Log in to reply
 

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