no additional space needed


  • 0
    K

    Basically eating away the digits from left and right.
    Corner cases, x<0 and x has a lot of zeros in left half of the number handled.

    class Solution {
    public:
    bool isPalindrome(int x) {
    if(x<0){return false;}
    while(x>9)
    {
    if(x/(int)(pow(10,floor(log(x)/log(10))))!=(x%10))
    {
    return false;
    }
    if((x%((int)(pow(10,floor(log(x)/log(10))))))<((int)(pow(10,floor(log(x)/log(10))-1))))
    {
    x=x+((int)(pow(10,floor(log(x)/log(10))-1)))+10;
    }
    x=(x%((int)(pow(10,floor(log(x)/log(10))))))/10;
    }
    return true;
    }
    };


  • 0
    W

    Same as mine.
    This is the place where using macros greatly helps for readability
    https://discuss.leetcode.com/topic/67843/an-accepted-solution-using-no-extra-variable


Log in to reply
 

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