原题:Determine whether an integer is a palindrome. Do this without extra space.
题意:判断数字是否是回文
思路:和上一篇 reverse Integer很类似,设该数字位num,先取到该数字的位数(如果个位计为1,10位计为10,依此类推),高位数字可以由 num/位数获得,低位数字可以由num % 10获得,比较高位数字和低位数字,更新num,直到num<10为止
public class Solution {
public boolean isPalindrome(int x) {
if(x < 0)
return false;
int numPow = 1;
while(x/numPow>=10)//获得位数
numPow *= 10;
int high = 0;//表示高位数字
int low = 0;//表示低位数字
while(x>=10)
{
high = x / numPow;
low = x % 10;
if(high != low)
return false;
x = (x % numPow - low)/10;
numPow /= 100;
}
return true;
}
}