Palindrome Number
Determine whether an integer is a palindrome without converting it to a string. A palindrome reads the same forwards and backwards (e.g., 121, 12321).
Key mathematical insight: negative numbers and numbers ending in 0 (except 0 itself) cannot be palindromes.
Solution
class Solution {
public:
bool isPalindrome(int x) {
// Negative numbers and numbers ending with 0 (except 0 itself) cannot be palindrome
if (x<0 || (x%10 == 0 && x!=0)) {
return false;
}
long long reverse;
int original = x;
while (x>0) {
int digit = x%10;
reverse = reverse*10 + digit;
x/=10;
}
return reverse==original;
}
};