题目:
Determine whether an integer is a palindrome. Do this without extra space.
思路:
不使用额外的空间判断一个数字是否是回文数字,例如1221是回文数字,而19990991不是。
依次求余数来构造一个新的数字并与原数字相比判断是否相等。
代码:
public class Solution {
public boolean isPalindrome(int x) {
//palindrome number回文数字:1221
if(x < 0){return false;}
//从末到头依次构造新数字,看是否与原数字相同
int n = 0;
int t = x;
while(x != 0){
n = n*10 + x%10;
x = x / 10;
}
return (t==n);
}
}