2022-04-29. palindrome-number 9.

2022-04-29  本文已影响0人  羲牧

翻转一半的数

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;    
    }
};
上一篇 下一篇

猜你喜欢

热点阅读