Question
Determine whether an integer is a palindrome. Do this without extra space.
Example
x = 121。
Solution
public class Solution {
public boolean isPalindrome(int x) {
if(x<0) return false;
int[] nums=new int[10];
int i=0;
while(x!=0){ //把x上的每个数字逐个存进数组nums
nums[i++]=x%10;
x/=10;
}
for(int j=0;j<i;j++){ //逐个遍历数组上的每个数,只需遍历至数组的中间即可
if(nums[j]!=nums[i-j-1])
return false;
}
return true;
}
}