You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
+
intpalindromeX = 0;
89
+
intinputX = x;
90
+
while(x>0){
91
+
palindromeX = palindromeX*10 + (x % 10);
92
+
x = x/10;
93
+
}
94
+
returnpalindromeX==inputX;
95
+
96
+
97
+
//要理解,纯粹的reverse integer会导致 overflow
98
+
//比如consider int overflow ? such as 1000000003 and 3000000001 is overflow......
0 commit comments