File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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+
Original file line number Diff line number Diff line change 1+
2+
You can’t perform that action at this time.
0 commit comments