题目描述:
给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。
解题:
计算出int的范围为[min,max],最后进行溢出判断
class Solution {
public:
int reverse(int x) {
int max=0x7fffffff,min=0x80000000;
long a=0;
while(x!=0){
a=a*10+x%10;
x=x/10;
}
return (a>max||a<min)?0:a;
}
};
执行结果: