C++ Solution


  • 0
    Y

    Calculate the number of bits. Then compare a pair of bits each time.
    For example "12321", compare '1' with '1' then '2' with '2'.

    class Solution {
    public:
        bool isPalindrome(int x) {
            if (x<0) {
                return false;
            }
            int numbit=0;
            int n=x;
            while (n>0) {
                n /= 10;
                numbit++;
            }
            for (int i=0; i<numbit/2; i++) {
                int right = x/pow(10,i);
                right %= 10;
                int left = x/pow(10,numbit-i-1);
                left %= 10;
                if (right != left) {
                    return false;
                }
            }
            return true;
        }
    };
    

Log in to reply
 

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