File tree 3 files changed +99
-0
lines changed
project/LeetCode/leetcode/src/main/java/com/blankj/easy/_009
3 files changed +99
-0
lines changed Original file line number Diff line number Diff line change 9
9
| 1| [ Two Sum] [ 001 ] | Array, Hash Table|
10
10
| 7| [ Reverse Integer] [ 007 ] | Math|
11
11
| 8| [ String to Integer (atoi)] [ 008 ] | Math, String|
12
+ | 9| [ Palindrome Number] [ 009 ] | Math|
12
13
13
14
14
15
## Medium
32
33
[ 001 ] : https://github.com/Blankj/awesome-java-leetcode/blob/master/note/001/README.md
33
34
[ 007 ] : https://github.com/Blankj/awesome-java-leetcode/blob/master/note/007/README.md
34
35
[ 008 ] : https://github.com/Blankj/awesome-java-leetcode/blob/master/note/008/README.md
36
+ [ 009 ] : https://github.com/Blankj/awesome-java-leetcode/blob/master/note/009/README.md
Original file line number Diff line number Diff line change
1
+ # [ Palindrome Number] ( https://leetcode.com/problems/palindrome-number/ )
2
+
3
+ ## Description
4
+
5
+ Determine whether an integer is a palindrome. Do this without extra space.
6
+
7
+ ** spoilers:**
8
+
9
+ ** Some hints:**
10
+
11
+ Could negative integers be palindromes? (ie, -1)
12
+
13
+ If you are thinking of converting the integer to string, note the restriction of using extra space.
14
+
15
+ You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?
16
+
17
+ There is a more generic way of solving this problem.
18
+
19
+ ** Tags:** Math
20
+
21
+
22
+ ## 思路0
23
+
24
+ 题意是判断一个有符号整型数是否是回文,也就是逆序过来的整数和原整数相同,首先负数肯定不是,接下来我们分析一下最普通的解法,就是直接算出他的回文数,然后和给定值比较即可。
25
+
26
+ ``` java
27
+ public class Solution {
28
+ public boolean isPalindrome (int x ) {
29
+ if (x < 0 ) return false ;
30
+ int copyX = x, reverse = 0 ;
31
+ while (copyX > 0 ) {
32
+ reverse = reverse * 10 + copyX % 10 ;
33
+ copyX /= 10 ;
34
+ }
35
+ return x == reverse;
36
+ }
37
+ }
38
+ ```
39
+
40
+ ## 思路1
41
+
42
+ 好好思考下是否需要计算整个长度,比如1234321,其实不然,我们只需要计算一半的长度即可,就是在计算过程中的那个逆序数比不断除10的数大就结束计算即可,但是这也带来了另一个问题,比如10的倍数的数10010,它也会返回` true ` ,所以我们需要对10的倍数的数再加个判断即可,代码如下所示。
43
+
44
+ ``` java
45
+ public boolean isPalindrome(int x) {
46
+ if (x < 0 || (x != 0 && x % 10 == 0 )) return false ;
47
+ int halfReverseX = 0 ;
48
+ while (x > halfReverseX) {
49
+ halfReverseX = halfReverseX * 10 + x % 10 ;
50
+ x /= 10 ;
51
+ }
52
+ return halfReverseX == x || halfReverseX / 10 == x;
53
+ }
54
+
55
+ ```
Original file line number Diff line number Diff line change
1
+ package com .blankj .easy ._009 ;
2
+
3
+ /**
4
+ * <pre>
5
+ * author: Blankj
6
+ * blog : http://blankj.com
7
+ * time : 2017/04/22
8
+ * desc :
9
+ * </pre>
10
+ */
11
+ public class Solution {
12
+ // public boolean isPalindrome(int x) {
13
+ // if (x < 0) return false;
14
+ // int copyX = x, reverse = 0;
15
+ // while (copyX > 0) {
16
+ // reverse = reverse * 10 + copyX % 10;
17
+ // copyX /= 10;
18
+ // }
19
+ // return x == reverse;
20
+ // }
21
+
22
+ public boolean isPalindrome (int x ) {
23
+ if (x < 0 || (x != 0 && x % 10 == 0 )) return false ;
24
+ int halfReverseX = 0 ;
25
+ while (x > halfReverseX ) {
26
+ halfReverseX = halfReverseX * 10 + x % 10 ;
27
+ x /= 10 ;
28
+ }
29
+ return halfReverseX == x || halfReverseX / 10 == x ;
30
+ }
31
+
32
+ public static void main (String [] args ) {
33
+ Solution solution = new Solution ();
34
+ System .out .println (solution .isPalindrome (-1 ));
35
+ System .out .println (solution .isPalindrome (10010 ));
36
+
37
+ System .out .println (solution .isPalindrome (0 ));
38
+ System .out .println (solution .isPalindrome (11 ));
39
+ System .out .println (solution .isPalindrome (111 ));
40
+ System .out .println (solution .isPalindrome (222222222 ));
41
+ }
42
+ }
You can’t perform that action at this time.
0 commit comments