Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
从低到高位,对每个数字乘以翻转后的权值,在求和就行了,代码如下:
class Solution {
public:
int reverse(int x) {
long ans=0;
while(x!=0){
ans=ans*10+x%10;
x/=10;
}
return (ans>INT_MAX||ans<INT_MIN)?0:ans;
}
};