贼坑的一道题;可能太久没写题目了,第一道就错了无数次
code:
class Solution {
public:
int reverse(int x) {
int y=0,tt=1;
if(x<0&&(x+INT_MAX)<0) return 0;
if(x<0) tt=-1,x*=-1;
while(x)
{
if((tt>0&&y>(INT_MAX-x%10*tt)/10)||(tt<0&&y<(INT_MIN-x%10*tt)/10)) return 0;
y=y*10+x%10*tt;
x/=10;
}
return y;
}
}; 
京公网安备 11010502036488号