Skip to content

Commit 505dddc

Browse files
authored
Update palindrome-number.cpp
1 parent 3407903 commit 505dddc

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

C++/palindrome-number.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,21 @@ class Solution {
1010
int temp = x;
1111
int reversed = 0;
1212
while (temp != 0) {
13+
if (isOverflow(reversed, temp % 10)) {
14+
return false;
15+
}
1316
reversed = reversed * 10 + temp % 10;
1417
temp = temp / 10;
1518
}
1619
return reversed == x;
1720
}
21+
22+
private:
23+
bool isOverflow(int q, int r) {
24+
static const int max_q = numeric_limits<int>::max() / 10;
25+
static const int max_r = numeric_limits<int>::max() % 10;
26+
return (q > max_q) || (q == max_q && r > max_r);
27+
}
1828
};
1929

2030
// Time: O(logx) = O(1)

0 commit comments

Comments
 (0)