Determine whether an integer is a palindrome. Do this without extra space.
public class Solution {
public boolean isPalindrome(int x) {
if(x<0) {
return false;
}
int pow = 1;
while(x/10>=pow) {
pow *= 10;
}
while(x!=0 && pow!=0) {
if(x%10!=(x/pow)%10) {
return false;
}
x = x/10;
pow = pow/100;
}
return true;
}
}
========
public class Solution {
public boolean isPalindrome(int x) {
if (x < 0) {
return false;
}
if (x < 10) {
return true;
}
int pow = 1;
while (x / 10 >= pow) {
pow *= 10;
}
while (pow != 0) {
int low = x % 10;
int high = x / pow;
if (low != high) {
return false;
}
x = (x - low * pow) / 10;
pow /= 100;
}
return true;
}
}
没有评论:
发表评论