Skip to content

Commit

Permalink
Update palindrome-number.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 committed Jan 16, 2019
1 parent 3407903 commit 505dddc
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions C++/palindrome-number.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,21 @@ class Solution {
int temp = x;
int reversed = 0;
while (temp != 0) {
if (isOverflow(reversed, temp % 10)) {
return false;
}
reversed = reversed * 10 + temp % 10;
temp = temp / 10;
}
return reversed == x;
}

private:
bool isOverflow(int q, int r) {
static const int max_q = numeric_limits<int>::max() / 10;
static const int max_r = numeric_limits<int>::max() % 10;
return (q > max_q) || (q == max_q && r > max_r);
}
};

// Time: O(logx) = O(1)
Expand Down

0 comments on commit 505dddc

Please sign in to comment.