读题心路历程:题读完,感觉好像也没那么难,我直接把int类型转换为string类型,然后reverse一下,再判断一下字符串前面是不是0,感觉就可以了。这个题可能复杂一点的就是int边界值的限制了。参考leetcode题解:图解 7. 整数反转 - 整数反转 - 力扣(LeetCode)
int reverse(int x) {
int ans = 0;
while (x!=0)
{
int tmp = x % 10;
if (ans > 214748364 || (ans == 214748364 && tmp > 7))
{
return 0;
}
if (ans < -214748364 || (ans == -214748364 && tmp < -8)) {
return 0;
}
ans = ans * 10 + tmp;
x = x / 10;
}
return ans;
}