9. Palindrome Number

key points

  • negative numbers
  • overflow
  • no extra space

The break point is sum >= x –> (sum ==x)||(sum/10==x)

source code

1
2
3
4
5
6
7
8
9
10
11
public boolean isPalindrome(int x) {
if(x < 0 || (x!=0 && x%10==0)){
return false;
}
int sum = 0;
while(x > sum){
sum = x%10 + sum*10;
x/=10;
}
return (sum ==x)||(sum/10==x);
}