Java Solution compare first position and last position each iteration without extra space.


  • 5
    M
    public class Solution {
        public boolean isPalindrome(int x) {
            if(x<0) {
                return false;
            }
    
            int k = 1;
            while(x/k>9){
                k = k * 10;
            }
    
            int y = x;
            while(y!=0) {
                if(x/k != y%10) {
                    return false;
                }
                x = x - (x/k)*k;
                y=y/10;
                k = k/10;
            }
    
            return true;
        }
    }

  • 0
    T

    I was also thinking about the same concept.
    However, I believe it's gonna fail for INPUT: 3002003


Log in to reply
 

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