Skip to content

Commit 6503f22

Browse files
committed
fix haoel#167 : need to check y is overflowed
1 parent 91556e4 commit 6503f22

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

Diff for: algorithms/cpp/palindromeNumber/palindromeNumber.cpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@
2626
class Solution {
2727
public:
2828

29-
bool isPalindrome(int x) {
29+
bool isPalindrome(int x) {
30+
return isPalindrome1(x);
31+
return isPalindrome2(x);
32+
}
33+
34+
bool isPalindrome1(int x) {
3035
if (x<0) {
3136
return false;
3237
}
@@ -59,6 +64,9 @@ class Solution {
5964
int n;
6065
while( x!=0 ){
6166
n = x%10;
67+
//check if y is overflowed or not.
68+
//if it is, then it's not palindrome
69+
if ( y > INT_MAX/10 - n) return x-1;
6270
y = y*10 + n;
6371
x /= 10;
6472
}

0 commit comments

Comments
 (0)