**模拟题**
关键点在于溢出的处理
```cpp
class Solution {
public:
/**
*
* @param x int整型
* @return int整型
*/
int reverse(int x) {
// write code here
int sign = 1, val = 0;
if(x < 0) sign = -1, x = -x;
while(x > 0){
int res = x % 10;
x /= 10;
if(sign == 1){
if(val > INT_MAX / 10 || (val == INT_MAX / 10 && INT_MAX % 10 < res)) return 0; //正溢出
}else{
if(val > INT_MAX / 10 || (val == INT_MAX / 10 && INT_MAX % 10 + 1 < res)) return 0; //负溢出
}
val = val * 10 + res;
}
return sign * val;
}
};
```
public:
/**
*
* @param x int整型
* @return int整型
*/
int reverse(int x) {
// write code here
int sign = 1, val = 0;
if(x < 0) sign = -1, x = -x;
while(x > 0){
int res = x % 10;
x /= 10;
if(sign == 1){
if(val > INT_MAX / 10 || (val == INT_MAX / 10 && INT_MAX % 10 < res)) return 0; //正溢出
}else{
if(val > INT_MAX / 10 || (val == INT_MAX / 10 && INT_MAX % 10 + 1 < res)) return 0; //负溢出
}
val = val * 10 + res;
}
return sign * val;
}
};
```