牛课题霸--palindrome-number

palindrome-number

题目链接

Solution

判断一个数字是否是回文串。
回文串的定义是正着读和反着读相同,所以我们可以把数字反转后,判断两个数字是否一样即可。
反转数字的方法是将n不断对10取模,然后除以10。

Code

class Solution {
public:
    bool isPalindrome(int x) {
        // write code here
        if (x < 0) return false;
        int tmp = x, y = 0;
        while (tmp) {
            y = y * 10 + tmp % 10;
            tmp /= 10;
        }
        return x == y;
    }
};
posted @ 2020-11-26 21:32  mjt233  阅读(58)  评论(0编辑  收藏  举报