标签:boolean while tostring || 方式 str for new return
public boolean isPalindrome(int x) {
if (x < 0) {
return false;
}
char[] chars = new String(Integer.toString(x)).toCharArray();
int length = chars.length;
for (int i = 0; i < length / 2; i++) {
if (chars[i] != chars[length - 1 - i]) {
return false;
}
}
return true;
}
public boolean isPalindrome(int x) {
if (x < 0) {
return false;
}
if (x != 0 && x % 10 == 0) {
return false;
}
int res = 0;
while (x > res) {
res = res * 10 + x % 10;
x /= 10;
}
return x == res || res / 10 == x;
}
标签:boolean while tostring || 方式 str for new return
原文地址:https://www.cnblogs.com/init-qiancheng/p/14626137.html