class Solution {
public:
int reverse(int x) {
int n = 0, temp;
bool flag = false;
if(x > 0){
flag = true;
x = -1 * x;
}
if(x == 0)
return x;
while(x){
temp = x % 10;
x = x / 10;
if(n < -214748364 || n == -214748364 && temp == 9)
return 0;
n = n * 10 + temp;
}
if(flag && n == -2147483648)
return 0;
return flag ? -1 * n : n;
}
};
Accepted
1032/1032 cases passed (4 ms)
Your runtime beats 49.1 % of cpp submissions
Your memory usage beats 75.11 % of cpp submissions (5.8 MB)