7.Reverse Integer
Input: 123
Output: 321
整数溢出后返回0 ,判断整数溢出:当整数溢出时,会得到一个随机的值,除以10,不会得到上一轮的结果;
class Solution {
public:
int reverse(int x) {
int ans=0,flag=0;
while(x){
flag=ans*10+x%10;
if(flag/10!=ans)
return 0;
ans=flag;
x/=10;
}
return ans;
}
};