class Solution {
public:
bool neg=false;
int reverse(int x) {
if(x<0){
neg=true;
x=x;
}
string s=to_string(x);
std::reverse(s.begin(),s.end());
int i=0;
while(s[i] == '0')
i++;
string s2(s.begin()+i,s.end());
int mx=INT_MAX;
string mxm=to_string(mx);
if(s2.size()==mxm.size() && s2 > mxm)
return 0;
return ((neg)?(1):(1))*stoi(s);
}
};
Don't know why failing INT_MIN() Test Case while it is passing INT_MAX test case.


Here is the surprising thing. When you do x = x, you get what you expect for all int32 except the int_min, where (2147483648) = 2147483648 or INT_MIN = INT_MIN. What is happening is that (2147483648) should be 2147483648, but 2147483648 is over the limit of int so you get an integer overflow. However instead of an integer overflow, c++ just returns INT_MIN back (abs() works the same way). This unexpected behavior later caused your stoi(s) to crash since it escaped your s2.size()==mxm.size() check.
In other words, you should treat INT_MIN as a special case or think of some other clever ways.