class Solution {
public:
int reverse(int x) {
short flag = x>0? 1:1;
int X = abs(x);
int res = 0;
while(X){
if((flag && (INT_MAXX%10)/10 < res)  (!flag && INT_MIN+res*10+X%10 > 0)){
return 0;
}
res = res*10 + X%10;
X /= 10;
}
return flag * res;
}
};
My c++ solution, handling overflow case

In the while loop, I don't know exactly what's the flag to do?
First, I use the following to get AC.
while (X) {
if( (INT_MAXX%10)/10 < res)){
return 0;
}
res = res*10 + X%10;
X /= 10;
}I know the job of the if condition, " res multi 10 plus X%10 is bigger than INT_MAX"——overflow.
QA:
What's (INT_MINX%10)/10 < res) mean. You get the absolute value of x as X. but in the while loop, you cosider the sign of the integer, may be something complicated happened?Can you explain what is purpose using (INT_MINX%10)/10 < res)?
Thanks.

Thanks for your comment!
First,the flag is the sign of x.
And getting the asolute value of x as X, is to make sure that X is always postive in the while loop, for the easier calculation.
Actually, INT_MAX is 2147483647, and INT_MIN is 2147483648
it means that abs(INT_MIN) = INT_MAX + 1, their numbers are different
so I think it can't use the same way to handle both postive case and negative case
therefore, when the flag is 1, which means the negative case, I use (INT_MINX%10)/10 < res) to handle the overflow.

Thanks for your explaination.
As the code you show, the value of res caculated can not overflow and is positive.
Given X is positive, INT_MIN  X%10 is overflowed, this is is bigger positive integer.
Can you explain why the condition (INT_MIN  X%10)/10 < res satisfied when the input is negitive, the revsed value is overflowed.
May be something like (INT_MIN + X%10)/10 =< res, I can understand this.
res10 +X%10 >= INT_MIN is the overlfowed condition, when input is negative.
This can reduce to
res10 >= INT_MINX%10
res >= (INT_MINX%10) / 10

oh, I know the problem you talk about!
I just don't remember how I think at that moment.
But I think you are right, something wrong with the code there, and I don't know why I get AC with this problem.
Maybe this way of the negative overflow is more reasonable:
(res*10 + X%10) < INT_MIN
and then INT_MIN+res*10+X%10 > 0
I will edit my code for the new way
Thanks for your suggestion!

If you use the following condition judged the overflow:
(INTMAX  X%1010*res) < 0,INTMAX  X%10 < 10*res
When it acctually overflows, 10res is negative, INTMAX  X%10 is positive, the condtion fails, it can not contains the overflow situation. The sign of (INTMAX  X%1010res) may be positive(can do some experiment), the condition fails, can not contains the overflow.
But use (INTMAXX%10)/10 < res, it is coducted from INTMAX < 10*res + X%10. transfrom to (INTMAXX%10)/10 < res, make the both side of the less equation are all positive, the same range. It can satify the purpose. And the value of res belongs to the corrent range.
These are my understand.