判斷一個整數(shù)是否是回文數(shù)误澳。回文數(shù)是指正序(從左向右)和倒序(從右向左)讀都是一樣的整數(shù)。
舉例: 121是回文數(shù)差导,123不是回文數(shù),-121不是回文數(shù)
思路:
1:負數(shù)不是回文數(shù)
2:正數(shù)是回文數(shù)的條件是反轉(zhuǎn)后的數(shù)字和反轉(zhuǎn)前的數(shù)字相等
public boolean isPalindrome(int x){
if(x<0){
return false;
}
return x == reverse(x);
}
public int reverse(int x){
int s = 0;
while(x>0){
s = s * 10 + x % 10;
x = x / 10;
}
return s;
}