9. Palindrome Number

Determine whether an integer is a palindrome. Do this without extra space.


解题思想:求得x的最高位,与x最低位比较。

我的代码:

class Solution {
public:
    bool isPalindrome(int x) {
        if(x<0)return false;
        if(x<10)return true;
        int n=1;
        for(;x/n>=10;n*=10);//找到n,使得x/n是x的最高位数字,x%n是x的最低位数字
        while(n>=10){
            if(x/n%10!=x%10)return false;
            x=x/10;//x去掉最低位
            n/=100;
        }
        return true;
    }
};


你可能感兴趣的:(LeetCode,C++)