Skip to content

Commit 9024b7d

Browse files
author
lingxiang.wang
committed
Merge branch 'master' of github.com:fifa0329/leetcode
2 parents 8f4e5f4 + 20f2b17 commit 9024b7d

File tree

2 files changed

+135
-15
lines changed

2 files changed

+135
-15
lines changed

PalindromeNumber.java

Lines changed: 107 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,113 @@
1414
*/
1515

1616
public class PalindromeNumber {
17+
18+
19+
1720
public boolean isPalindrome(int x) {
18-
if (x < 0)
19-
return false;
20-
int k = 1;
21-
while (x / k >= 10) {
22-
k *= 10;
23-
}
24-
while (x >= 10) {
25-
int left = x / k;
26-
int right = x - x / 10 * 10;
27-
if (left != right)
28-
return false;
29-
x = (x - x / k * k) / 10;
30-
k /= 100;
31-
}
32-
return true;
21+
//压根不会做,看了答案
22+
23+
//下面方法是错的!
24+
// if (x < 0)
25+
// return false;
26+
// int k = 1;
27+
// while (x / k >= 10) {
28+
// k *= 10;
29+
// }
30+
// while (x >= 10) {
31+
// int left = x / k;
32+
// int right = x - x / 10 * 10;
33+
// if (left != right)
34+
// return false;
35+
// x = (x - x / k * k) / 10;
36+
// k /= 100;
37+
// }
38+
// return true;
39+
//我复制了这个答案 ,发现这个就是错的!
40+
//10进制的重要的点,因为数字未知,一开始应该用while来判断出位数,通过不断的十进制乘上去
41+
//第一位和最后一位的判断相等,思路还是很清晰的
42+
//但是继续进行,判断是否回文,就有点乱了
43+
44+
45+
46+
47+
// ------------------------
48+
49+
50+
//如何通过进制,来进行int的reverse
51+
52+
/*
53+
54+
very nice try!
55+
56+
commented Oct 10, 2014 by dingou666
57+
58+
consider int overflow ? such as 1000000003 and 3000000001 is overflow......
59+
60+
commented Oct 12, 2014 by yomin
61+
62+
Amazing! Just add check condition as follows and u are done. if(x < 0 || x == Integer.MAX_VALUE) return false;
63+
64+
commented Oct 12, 2014 by erenalgan
65+
66+
Good point! Actually I did not consider overflow when writing the code... But it turns out that overflow is automatically handled: palindromeX will result in a negative number because of overflow, and that makes it never equals to the input number x...
67+
68+
commented Oct 12, 2014 by hln9319
69+
70+
Same concern, it will be overflow for some case.
71+
72+
commented Dec 2, 2014 by windkiosk
73+
74+
use long long to store palindromeX
75+
76+
commented Dec 17, 2014 by zhanglg921
77+
78+
OJ considers negative numbers as non-palindrome. Though, your solution does not handles this case explicitly - seems to me that it is taken care of as a side effect of your algorithm. For ex: if x = -121, palindromeX = 121 by the end of the for loop which is not equal to x (-121). But, there is really no need to go through the for loop, you can return false if x < 0. Otherwise, very neat solution!
79+
80+
commented Dec 29, 2014 by rainhacker
81+
82+
*/
83+
84+
85+
86+
//Reverse Integer与这道题异曲同工
87+
88+
int palindromeX = 0;
89+
int inputX = x;
90+
while(x>0){
91+
palindromeX = palindromeX*10 + (x % 10);
92+
x = x/10;
93+
}
94+
return palindromeX==inputX;
95+
96+
97+
//要理解,纯粹的reverse integer会导致 overflow
98+
//比如consider int overflow ? such as 1000000003 and 3000000001 is overflow......
99+
100+
//为什么一次通过?因为测试的case不包含这种特殊例子
101+
102+
103+
104+
105+
/* 一般用更大的类型去处理是一个思路!!!
106+
bool isPalindrome(int x) {
107+
long reverse = 0;
108+
long num = abs(x);
109+
while(x != 0){
110+
reverse *= 10;
111+
reverse += x % 10;
112+
x /= 10;
113+
}
114+
return reverse == num;
115+
}
116+
*/
117+
118+
119+
120+
121+
122+
123+
124+
33125
}
34126
}

ReverseInteger.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,34 @@
1919

2020
public class ReverseInteger {
2121
public int reverse(int x) {
22+
23+
24+
25+
26+
27+
28+
29+
30+
31+
32+
33+
34+
35+
36+
37+
38+
39+
40+
41+
42+
43+
44+
45+
46+
47+
48+
49+
2250
int num = Math.abs(x);
2351
int ret = 0;
2452
while (num != 0) {

0 commit comments

Comments
 (0)