牛课题霸--palindrome-number
回文数字
https://www.nowcoder.com/practice/35b8166c135448c5a5ba2cff8d430c32?tpId=117&&tqId=34977&rp=1&ru=/ta/job-code-high&qru=/ta/job-code-high/question-ranking
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;
}
}; 

