美文网首页
2022-04-29. palindrome-number 9.

2022-04-29. palindrome-number 9.

作者: 羲牧 | 来源:发表于2022-04-29 11:47 被阅读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;    
        }
    };
    

    相关文章

      网友评论

          本文标题:2022-04-29. palindrome-number 9.

          本文链接:https://www.haomeiwen.com/subject/pwksyrtx.html