来源:力扣 (LeetCode)
链接:https://leetcode-cn.com/problems/reverse-integer
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
一、题目描述
给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。
示例:
- 输入: 123
- 输出: 321
示例 2:
- 输入: -123
- 输出: -321
示例 3:
- 输入: 120
- 输出: 21
二、题解
题目很简单,基本的思路就是对 n 循环取模,然后乘 10 累加。但是问题的关键点不是在这里,关键点是数据溢出。假设传入数据比 INT_MAX 小,但是反转后比 INT_MAX 大,那就会产生溢出。
INT_MAX = 2147483647, INT_MIN = -2147483648;
假设反转后的值是 output,被反转数字模 10 后得到 y,那么 output 应该被设置为 output * 10 + y 。
此时 output 溢出的条件:
output > INT_MAX / 10或者output = INT_MAX / 10 && y > 7。output < INT_MIN / 10或者output = INT_MIN / 10 && y < -8。
以第一点为例,假设此时 output 大于最大值除以 10,那么它乘以 10 后肯定会大于最大值,也就会溢出。如果它等于最大值除 10,那么只要 y 大于最大值取模,也会溢出。
三、代码
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class Solution { public: int reverse(int x) { int output = 0, y; while (x) { y = x % 10; if (output > INT_MAX / 10 || (output == INT_MAX / 10 && x > 7)) return 0; if (output < INT_MIN / 10 || (output == INT_MIN / 10 && x < -8)) return 0; output = output * 10 + y % 10; x /= 10; } return output; } }; |
![[leetcode]7-整数反转](https://www.dyxmq.cn/wp-content/uploads/2020/02/e23c0-image.png)
![[leetcode]125-验证回文串](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/5bfa5-image.png&w=280&h=210&a=&zc=1)
![[leetcode]704-二分查找](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/02/3f229-image4d7f5fcd65024b40.png&w=280&h=210&a=&zc=1)
![【每日打卡】[leetcode]面试题1.6-字符串压缩](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/776bc-image.png&w=280&h=210&a=&zc=1)
![[leetcode]410-分割数组的最大值](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/02/a851e-image00f04be2c08aea1c.png&w=280&h=210&a=&zc=1)

![[leetcode]199-二叉树的右视图](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/04/f24a8-image.png&w=280&h=210&a=&zc=1)
![【每日打卡】[leetcode]72-编辑距离](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/04/88c60-imageff84a6c5047db6ed.png&w=280&h=210&a=&zc=1)
![【每日打卡】[leetcode]460-LFU缓存](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/04/5b3f2-image.png&w=280&h=210&a=&zc=1)
![【每日打卡】[程序员面试宝典]17.16-按摩师](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/9ecc9-image.png&w=280&h=210&a=&zc=1)
![【每日打卡】[leetcode]876-链表的中间节点](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/64d0d-imagece778ab033c9d90d.png&w=280&h=210&a=&zc=1)
![【每日打卡】[剑指offer]面试题40-最小的k个数](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/c83c9-imagec745c425c670e18c.png&w=280&h=210&a=&zc=1)
![【每日打卡】[leetcode]-409最长回文子串](https://www.dyxmq.cn/wp-content/themes/begin/prune.php?src=https://www.dyxmq.cn/wp-content/uploads/2020/03/e5589-image788d511d7c1e839c.png&w=280&h=210&a=&zc=1)
评论