Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
click to show spoilers.
Note:
The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.
简单题,不解释。
class Solution {
public:
int reverse(int x) {
long result=0;
while(x!=0)
{
result=result*10+x%10;
if(result>(1<<31)-1||result<-(1<<31))return 0;
x=x/10;
}
return (int)result;
}
};