回文数

题目

https://leetcode-cn.com/problems/palindrome-number/

解法

除非必要,不然尽量减少 出口条件判断; 正常的结果能一起就一起判断,异常早点判断。对数操作之后,尽量早点去判断掉 他的边界情况

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public boolean isPalindrome(int x) {
if (x < 0 || x%10==0&& x != 0) return false;
// if (x <= 9) return true;
int num = x;
int modNum = 0;
while(num > modNum) {
// if (num - modNum * 10 > 0 && num - modNum * 10<9) break;
modNum = 10*modNum + num%10;
num /= 10;
}
if(modNum == num || num == modNum/10) {
return true;
} else {
return false;
}

}
}
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×