LeetCode-9 Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space.
方法一:直接转String倒置
public class Solution {
public boolean isPalindrome(int x) {
if (x < 0) {
return false;
}
String xstr = String.valueOf(x);
for (int i = 0; i < xstr.length()/2; i++) {
if (xstr.charAt(i) == xstr.charAt(xstr.length()-1-i)) {
continue;
}else {
return false;
}
}
return true;
}
}
Runtime: 446 ms
方法二:
public class Solution {
public boolean isPalindrome(int x) {
if (x < 0) {
return false;
}
int n = x;
int x1 = x, x2 = x;
int i = 0;//位数
int pera = 0;
int next = 0;
for (i = 0; n > 0; i++) {
n /= 10;
}
for (int j = i; j > i / 2; j--) {
pera = (int) (x1 / Math.pow(10, j - 1));
next = x2 % 10;
if (pera == next) {
x1 = (int) (x1 % Math.pow(10, j - 1));
x2 = x2 / 10;
} else {
return false;
}
}
return true;
}
}
Runtime: 421 ms