# c ++ – Reverse int in the 32-bit signed integer range: \$[−2^{31}, 2^{31} − 1] \$ Optimized

LeetCode problem

Inverted digits of a 32-bit signed integer. When the inverted integer overflows, returns 0.

Return of information

Optimized from the beta code in the original question here. On the basis of this problem LeetCode. I'm trying to come up with a good approach that simply involves doing mathematical operations to iterate the numbers using the division, almost treating the `int` like a `stack` poping using the module, and pushing using multiplication. I think that there may still be overflow cases that I do not capture as `1534236469`, but you need help finding how to detect and prevent such cases.

``````#understand
#understand
#understand
#understand

class solution
{
public:
int reverse (int i) {

if (i> INT_MAX || i <INT_MIN) {
returns 0;
}
if I < 0) {
sign = -1;
i = i*sign;
}

int reversed = 0;
int pop = 0;

while(i > 0) {
pop = i% 10;
inverted = inverted * 10 + pop;
i / = 10;
}

std :: cout << inverted << & nbsp ;;

return the * reversed sign;
}
};

int main ()
{
solutions;

assert (s.reverse (1) == 1);
assert (in inverse (0) == 0);
assert (in inverse (123) = 321);
assert (in inverse (120) = 21);
assert (conversely (-123) == -321);
assert (in inverse (1207) = 7021);
assert (upside down (1534236469) == 0);
assert (in inverse (-2147483412) == -2143847412);
}
``````