# 8 ms simple C++ solution which checks overflow

• ``````class Solution {
public:
int reverse(int x) {
int ans = 0;
while (x) {
int temp = ans * 10 + x % 10;
if (temp / 10 != ans)
return 0;
ans = temp;
x /= 10;
}
return ans;
}
};``````

• This post is deleted!

• you can define variable `ans` as `long` type and compare the final value of `ans` with INT_MAX and INT_MIN in the end of your code once.

• Can you tell me why this code checks overflow?

• What if ans overflows the long type?

• I try to give a simple explanation here.

For example, if 'b = a * 10' causes overflow, 'b' will be equal to a strange pseudo-random number. So 'b/10' will not be equal to 'a' anymore.

• not possible. Input number is an integer, it causes imbalance overflow only when the input is a 10-digit number(i.e.Input 1234567893 and output 3987654321).

• I believe this solution invokes undefined behavior. Signed integer overflow is undefined so you are making assumptions based on UB. Someone feel free to correct me.

• 8 ms solution

Lies. It takes 35 milliseconds.

• @kirkdokuzelli Yes. I saw they must have updated test cases. Then the solution one year ago couldn't remain 8ms. Does it make sense? LOL :)

• @kirkdokuzelli Yes. I saw they must have updated test cases. Then the solution one year ago couldn't remain 8ms. Does it make sense? LOL :)

While the fact that this works is awesome, it's performance is among the worst. No offense.

• @kirkdokuzelli
I tried using 8 bytes long integer solution but can only get 15 ms runtime.

``````class Solution {
public:
int reverse(int x) {
long long ans = 0;
while (x) {
ans = ans * 10 + x % 10;
x /= 10;
}
return ans < INT_MIN || ans > INT_MAX ? 0 : ans;
}
};
``````

I think maybe most of the low runtime solutions were submitted a year ago.

Another of my solution is just a little better. I post it here FYI.

``````class Solution {
public:
int reverse(int x) {
int ans = 0, lo = INT_MIN / 10, hi = INT_MAX / 10;
while (x) {
if (ans < lo || ans > hi)
return 0;
int r = x % 10;
ans = ans * 10;
if (r < 0 && ans < INT_MIN - r || r > 0 && ans > INT_MAX - r)
return 0;
ans += r;
x /= 10;
}
return ans;
}
};
``````

• @Aeonaxx `INT_MIN` and `INT_MAX` is the old C style way. In C++, it's better to use `std::numeric_limits<int>::min()` and `std::numeric_limits<int>::max()`

• @njbelles right. Thank you for pointing it out :)

• How does this deal with negative numbers?
For example, -123 % 10 would be 7 because it would essentially be -3 % 10, which doesn't equal -3 but equals 7

• @rosita I tried on my machine. -3 % 10 is -3. not 7.

• @Aeonaxx interesting... because if you open a python console and try -3 % 10, it's 7. I even Googled it and -3 % 10 = 7 lol. How did you check it on your machine?

• @rosita It looks python is different for mod. I tested in C++ (Xcode) and Java (IntelliJ).

• @bianlu because that sentence /* if (temp / 10 != ans) return 0; */
if temp overflows, temp / 10 != ans, and it will return 0

• anyone who can explain me why this code is needed?
if (temp / 10 != ans)
return 0;

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.