题目链接:https://leetcode.com/problems/reverse-integer/
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
思路
注意溢出情况处理
class Solution {
public:
int reverse(int x) {
int result = 0;
while(x){
if(INT_MAX/10 < result || INT_MIN/10 > result)
return 0;
result = result*10 + x % 10;
x = x / 10;
}
return result;
}
};