Skip to content

Commit 6b88dad

Browse files
committed
回文数判断
1 parent f4049a8 commit 6b88dad

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@
196196
- [Partition Array](/algorithm/LeetCode/Array/Partition-Array.md)
197197
- [Subarray Sum](/algorithm/LeetCode/Array/Subarray-Sum.md)
198198
- [Plus One](/algorithm/LeetCode/Array/plus-one.md)
199+
- [Palindrome Number](/algorithm/LeetCode/Array/Palindrome-Number.md)
199200
- [String](/algorithm/LeetCode/String.md)
200201
- [Restore IP Addresses](/algorithm/LeetCode/String/ip.md)
201202

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
## 一、题目
2+
3+
> Determine whether an integer is a palindrome. Do this without extra space.
4+
5+
给定一个数字,要求判断这个数字是否为回文数字. 比如121就是回文数字,122就不是回文数字.
6+
7+
## 二、解题思路
8+
9+
题目要求只能用O(1)的空间,所以不能考虑把它转化为字符串然后reverse比较的方法。
10+
11+
基本思路是每次去第一位和最后一位,如果不相同则返回false,否则继续直到位数为0。
12+
13+
需要注意的点:
14+
15+
1. 负数不是回文数字.
16+
2. 0是回文数字.
17+
18+
## 三、解题代码
19+
20+
```java
21+
public boolean isPalindrome(int x) {
22+
if(x<0)
23+
return false;
24+
int div = 1;
25+
while(div<=x/10)
26+
div *= 10;
27+
while(x>0)
28+
{
29+
if(x/div!=x%10)
30+
return false;
31+
x = (x%div)/10;
32+
div /= 100;
33+
}
34+
return true;
35+
}
36+
```
37+

android/advance/相关事迹.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+

0 commit comments

Comments
 (0)