Palindrome Number

思路:将元素从右向左用霍纳法则相加,得到结果。如果有溢出,r和x不会相等。

class Solution {
public:
    bool isPalindrome(int x) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if (x == 0) return true;
        else if (x<0) return false;
        int r = 0, t = x;
        while (t>0) {
            r = r*10+t%10;
            t/=10;
        }
        return r==x;
    }
};


你可能感兴趣的:(Palindrome Number)