Hi everyone, I am "@不飞的小飞驴"
2 Given a 32-bit signed integer, you need to reverse the digits on each of the integers.
Example: Given input: 123 output: 321
input: -123 output: -321
input: 120 output: 21
Note: Assuming that our environment can only store the next 32-bit signed integer, the value range is [-2 31 , 2 31-1]. According to this assumption, if the integer overflows after the inversion, it returns 0.
First of all, what solutions do you think of, please comment below.
The overall idea: Use %10 and /10 to constantly take out the number corresponding to the position, and then perform an operation to reverse it.
int reverse(int x){
int tmp=0;
while(x!=0){
tmp=tmp*10+x%10;
x=x/10;
}
if(tmp>(1<<31-1)||tmp<-(1<<31)){
return 0;
}
return tmp;
}