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

  • 3
    class Solution {
        bool isPalindrome(int x) {
                return false;
            int dc=0, t=x, temp=0; //dc is for digit count in x
            while(t){ //counting no of digits in x
                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

    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.