7.整数反转
https://leetcode-cn.com/problems/reverse-integer/
思路一致但写的有点复杂,懒得改了。无需做正负判断。int最大最小值可以用宏INT_MAX和INT_MIN代替。
class Solution {
public:
int reverse(int x) {
int i=0,bit=0,flag=0;
int res=0,mod=0;
flag = x>0?1:0;
if(x==-2147483648){return 0;}
x=abs(x);
while((mod=x%10)==0&&x/10>0){ x=x/10;}
while((mod=x%10)>0||((mod=x%10)==0&&x/10>0)){
bit++;
if(bit>=10){
if(res>214748364||(res==214748364)&&mod>7)return 0;
}
res=res*10+mod;
x=x/10;
}
res=flag==1?res:-res;
return res;
}
};