LeetCode 9. Palindrome Number
Given an integer x, return true if x is palindrome integer.
An integer is a palindrome
when it reads the same backward as forward. For example, 121 is palindrome while 123 is not.
示例 1:
输入:x = 121
输出:true
示例 2:
输入:x = -121
输出:false
解释:从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。
method
bool isPalindrome(int x) {
int num = x;
if (num < 0) return false;
int res = 0;
while (num) {
if (res > INT_MAX / 10) return false; // 反转后溢出
res = res * 10 + num % 10;
num /= 10;
}
return res == x;
}