JAVA solve the INT version problem like a String (O(1) space) #beats 95% 183ms#


  • 0
    B
    class Solution {
        public boolean isPalindrome(int x) {
            if(x < 0)
                return false;
            int l = 0;
            int y = x;
            while(y != 0){
                y = y / 10;
                l++;
            }
            while(l > 1){
                int digit = (int) Math.pow(10, l-1);
                int high = x / digit;
                x -= high * digit;
                int low = x % 10;
                x = x /10;
                if(high != low)
                    return false;
                l = l - 2;
            }
            return true;
        }
    }
    

Log in to reply
 

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