https://leetcode.com/problems/palindrome-number/?tab=Description
Determine whether an integer is a palindrome. Do this without extra space.
//method 1
bool isPalindrome(int x) {
int y = 0, tmp = x;
if(x < 0) return false;
while(tmp) {
y*=10;
y += tmp%10;
tmp/=10;
}
return x==y;
}
//method 2
bool isPalindrome(int x) {
char buf[11] = {0};
int cur = 0;
if(x < 0) return false;
while(x) {
buf[cur++] = x%10;
x/=10;
}
for(int i = 0; i < (cur>>1); i++) {
if(buf[i] != buf[cur - i - 1]) return false;
}
return true;
}
//method 3
bool isPalindrome(int x) {
if (x<0 || (x!=0 && x%10==0)) return false;
int rev = 0;
while (x>rev){
rev = rev*10 + x%10;
x = x/10;
}
return (x==rev || x==rev/10);
}