My accepted C++ Solution used extra space. Comment to improve it.


  • 3
    K
    class Solution {
    public:
        bool isPalindrome(int x) {
            int rem, rev=0;
            if(x<0)
                return false;
            if(x==0)
                return true;
            int y=x;
            while(x != 0)
            {
                rem=x%10;
                rev=rev*10+rem;
                x=x/10;
            }
            if(y==rev)
                return true;
            else
                return false;
        }
    };

  • 0
    A

    Brilliant! I think "extra space" actually means space related to input size, so your answer is great.


  • 0
    S

    i agree with you , no extra space means O(1) space.


  • 0
    S

    great, a different thought, really open my mind.


  • 2
    J

    although your code can get AC,,,,,,,
    but ......it may casue overflow.....


Log in to reply
 

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