2022-04-29. palindrome-number 9. 回文数

翻转一半的数

class Solution {
public:
    bool isPalindrome(int x) {
        if (x == 0){
            return true;
        }
        if (x < 0 || x % 10 == 0){
            return false;
        }
        int revertedNum = 0;
        while (x > revertedNum){
            revertedNum = revertedNum * 10 + x % 10;
            x = x/10;
        }
        // cout << x << " " << revertedNum << endl;
        return x == revertedNum || x == revertedNum/10;    
    }
};

你可能感兴趣的:(2022-04-29. palindrome-number 9. 回文数)