I have been coding on leetcode to reverse the number in a 32-bit environment. This error is popping up for test case where x=-2147483648. I have seen other solutions for the same problem and i understand them but i just want to know why this code is wrong..
int reverse(int x) {
if (x==0){
return 0;
}
bool n = false;
if (x<0){
x = -x;
n = true;
}
long num = 0,count =0;
while (x>0){
int rem;
rem = x%10;
x = x/10;
if ( (num*10) > (pow(2,31)-1) || (num*10) < -pow(2,31)){
return 0;
}
num = num*10 + rem;
}
if(n==true){
num = -num;
}
return num;
}
this is the code i tried
New contributor
Ayush Saraf is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1