LeetCode 7. Reverse Integer

Solution1:最笨的方法

class Solution {public:int reverse(int x) {if (!x) return x;int flag = x > 0? 1: -1;long long res = 0;int temp_x = abs(x);while (temp_x) {res = res * 10 + (temp_x % 10);temp_x /= 10;}res *= flag;if (res > INT_MAX || res < INT_MIN)return 0;return res;}
};

Solution2:(其实不咋好理解。。)
参考网址:http://www.cnblogs.com/grandyang/p/4125588.html
提交通过后,OJ给出了官方解答,一看比自己的写的更精简一些,它没有特意处理正负号,仔细一想,果然正负号不影响计算,而且没有用long long型数据,感觉写的更好一些,那么就贴出来吧:

class Solution {public:int reverse(int x) {int res = 0;while (x != 0) {if (abs(res) > INT_MAX / 10) return 0;res = res * 10 + x % 10;x /= 10;}return res;}
};

在贴出答案的同时,OJ还提了一个问题 To check for overflow/underflow, we could check if ret > 214748364 or ret < –214748364 before multiplying by 10. On the other hand, we do not need to check if ret == 214748364, why? (214748364 即为 INT_MAX / 10)
为什么不用check是否等于214748364呢,因为输入的x也是一个整型数,所以x的范围也应该在 -2147483648~2147483647 之间,那么x的第一位只能是1或者2,翻转之后res的最后一位只能是1或2,所以res只能是 2147483641 或 2147483642 都在int的范围内。但是它们对应的x为 1463847412 和 2463847412,后者超出了数值范围。所以当过程中res等于 214748364 时, 输入的x只能为 1463847412, 翻转后的结果为 2147483641,都在正确的范围内,所以不用check。
Solution3:好法!
参考网址:https://blog.csdn.net/m0_37454852/article/details/78148840

class Solution {public:int reverse(int x) {int y = 0;//需要返回的数while (x) {int temp = y;//暂存y的值y = y * 10 + x % 10;//倒序;把x的最低位依次压入y的最低位if((y-x%10)/10!=temp)//反向推,若推不出原值则溢出return 0;x/=10;}return y;}
};

总结: 如何判断是否溢出?
其实看我上面的代码也可以看得出了,只要把这个式子反着推过来,再来看是否相等就行了。
1.加法溢出判断:若c=a+b; c-a!=b则溢出;或者a, b>0, c<0溢出;或者a, b<0, c>0溢出;
2.减法溢出判断:若c=a-b; c+b!=a则溢出;
3.除法溢出判断:若b!=0 && a/b=c; b*c!=a则溢出;
4.乘法溢出判断:若c=a*b; a!=0 && c/a!=b则溢出。

【翻转整数考虑溢出】LeetCode 7. Reverse Integer相关推荐

  1. LeetCode之Reverse Integer

    1.题目 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 cli ...

  2. LeetCode 7 Reverse Integer(反转数字)

    题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...

  3. LeetCode - 7. Reverse Integer

    7. Reverse Integer Problem's Link ------------------------------------------------------------------ ...

  4. LeetCode 7. Reverse Integer

    问题链接 LeetCode 7 题目解析 给定一个32位有符号整数,求其反转数字. 解题思路 如果是简单反转的话,那这道题就太简单了.题目要求判断溢出问题,32位int类型的范围是-214748364 ...

  5. LeetCode - 7 - Reverse Integer

    题目 URL:https://leetcode.com/problems/reverse-integer 解法 这个题目是极其简单的,对于数 x,每次对 10 取余保存为结果,之后 x 除以 10,若 ...

  6. LeetCode——7. Reverse Integer

    一.题目链接:https://leetcode.com/problems/reverse-integer/ 二.题目大意: 给定一个整数,要求反转该整数之后再返回:如果归返回的整数超过了int型整数的 ...

  7. leetcode No7. Reverse Integer

    Question: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -32 ...

  8. leetcode 7 Reverse Integer

    class Solution { public:int reverse(int x) {int res = 0;while (x != 0) {int t = res * 10 + x % 10;if ...

  9. [LeetCode] Number of 1 Bits Reverse Integer - 整数问题系列

    目录: 1.Number of 1 Bits  - 计算二进制1的个数 [与运算] 2.Contains Duplicate - 是否存在重复数字 [遍历] 3.Reverse Integer - 翻 ...

最新文章

  1. 伍六七带你学算法 入门篇-卡牌分组
  2. 2021-10-14 yolov5踩坑!!!经验大赏
  3. matlab腐蚀膨胀代码_(三十二)形态学----膨胀和腐蚀
  4. 一个关于malloc的面试题
  5. android 测试网,Android Monkey测试
  6. escplise使用教程_eclipse使用教程
  7. DNS的几个基本概念:
  8. Arthas-MathGame
  9. [转]ACM之java速成
  10. linux命令行启动新终端,12个让您震撼的Linux终端命令
  11. SketchUp资源网站
  12. 网站压力测试的几种方法
  13. java jdom 创建xml_java中使用jdom生成xml
  14. PDF文件双面打印设置
  15. chrome调试微信网页_2022.2.9可行
  16. 莫烦Python--Tensorflow Day5
  17. (私人收藏)PPT数据图表
  18. iOS代码混淆安全加固
  19. Computer Architectrure: Quantitative Approch 第三章第九节
  20. php制作万年历的步骤_使用PHP制作一个万年历

热门文章

  1. windows Server 2008+iis 7.5 部署应用程序
  2. 窗口封装类与Windows窗口实例的关系-3、CWnd如何处理窗口消息
  3. *******clob问题***********
  4. pytorch---之随机种子初始化
  5. 谜底是计算机的谜语英语,英语谜语(Riddle)  谜底
  6. python调试方法logging_python中logging使用方法
  7. 微信 的微服务器配置,spring-boot wm-accesstoken
  8. python的字符串类型_python如何判断某变量是否为字符串类型
  9. 克隆卡设备_SD Clone for mac(SD卡克隆备份软件) v3.2
  10. java 静态方法 变量_Java变量的初始化及静态方法的实现