问题描述
这是 leetcode 问题 9
class Solution {
public boolean ispalindrome(int x) {
if(x<0 || (x!= 0 && x%10 == 0) ){
return false;
}
int res = 0;
while(x>res){
res = res*10 + x % 10;
if(x == res || x == res/10){
return true;
}
x = x/10;
}
return false;
}
}
if(x == res || x == res/10){
return true;
}
这部分,如果return true,while循环结束后会有很多true出来。然而,一个返回是否应该停止整个程序?
解决方法
您实际上是在尝试找到字符串的中点并查看 2 位数字是否匹配。
示例: 假设数字是 5225。迭代看起来像这样:
分辨率 x
- 5 5225
- 52 522 这里 res==x/10 -> 返回 true。