Determine whether an integer is a palindrome. Do this without extra space.
click to show spoilers.
Palindrome Number的意思就是:对称的数字。负数不算。
public class Solution {
public boolean isPalindrome(int x) {
if (x<0)
return false;
int k = 0;
int y = x;
while(y>0){
y /= 10;
k++;
}
for(int i = 1; i <= k/2; i++){
int j = k+1-i;
if(digitAt(x,i) != digitAt(x,j))
return false;
}
return true;
}
}
但是关于这个代码有一个问题。如果最后一个函数写成这样。。。就会报错。说不能double 转int,但是这是为什么呢??来个大神告诉我吧=。=oh my god。
public int digitAt (int x, int n){
x = x/Math.pow(10,n-1);
return x%10;
}