/*
给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。
示例 1:
输入: 123
输出: 321
示例 2:
输入: -123
输出: -321
示例 3:
输入: 120
输出: 21
*/
#include <vector>
#include <limits.h>
using namespace std;
#define max(a,b) (((a) > (b)) ? (a) : (b))
#define min(a,b) (((a) < (b)) ? (a) : (b))
class Solution {
public:
int reverse(int x)
{
int overturn = 0;//反转数
while (x != 0)//翻转n-1次
{
int remaind = x % 10;
if (overturn > INT_MAX / 10 || (overturn == INT_MAX / 10 && remaind > 7))//当n-1时,十位数大于最大十位数;当n-1时,翻转数大于最大数;
return 0;
if (overturn < INT_MIN / 10 || (overturn == INT_MIN / 10 && remaind < -8))//相反
return 0;
overturn = overturn * 10 + remaind;
x /= 10;
}
return overturn;
}
};
int main()
{
Solution solution;
int ret = solution.reverse(512);
printf("%d ---- %d\n", INT_MAX,INT_MIN);
printf("ret ---- %d\n", ret);
getchar();
return 0;
}