48ms c++ solution, very clean and commented code with no extra space


  • 3
    R
    class Solution {
    public:
        bool isPalindrome(int x) {
            if(x<0)
                return false;
            int dc=0, t=x, temp=0; //dc is for digit count in x
            while(t){ //counting no of digits in x
                dc++;
                t /=10;
            }
            for(int i=0;i<dc/2;i++){ //just traverse half of digit and create a new number with dc/2 digits only
                temp = 10*temp+x%10;
                x /=10;
            }
            if(dc%2)// if dc is odd drop one more digit from x
                x /=10;
            return x==temp;
        }
    };

  • 0
    M

    Clever solution without using long long int.


Log in to reply
 

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