No extra space, no overflow, my 72ms solution


  • 0
    J
    class Solution {
    public:
        bool isPalindrome(int x) {
            if (x < 0)
                return false;
            if (x < 10)
                return true;
    
            int len = 0, t = x;
            while(t)
            {
                t /= 10;
                len++;
            }
            
            int high, low;
            while(len > 1)
            {
                high = x / pow10(1, len-1);
                low = x % 10;
                if(high != low)
                    return false;
                
                x = x % pow10(1, len-1);
                x = x / 10;
                len -= 2;
            }
            
            return true;
        }
        
        int pow10(int n, int e)
        {
            while(e--)
                n *= 10;
            return n;
        }
    };

Log in to reply
 

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