[LeetCode] Reverse Integer 翻转整数
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
- Input: 123
- Output: 321
Example 2:
- Input: -123
- Output: -321
Example 3:
- Input: 120
- Output: 21
Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
解法一:
- class Solution {
- public:
- int reverse(int x) {
- int res = ;
- while (x != ) {
- if (abs(res) > INT_MAX / ) return ;
- res = res * + x % ;
- x /= ;
- }
- 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 / )
为什么不用 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。
我们也可以用 long 型变量保存计算结果,最后返回的时候判断是否在 int 返回内,但其实题目中说了只能存整型的变量,所以这种方法就只能当个思路扩展了,参见代码如下:
解法二:
- class Solution {
- public:
- int reverse(int x) {
- long res = ;
- while (x != ) {
- res = * res + x % ;
- x /= ;
- }
- return (res > INT_MAX || res < INT_MIN) ? : res;
- }
- };
Github 同步地址:
https://github.com/grandyang/leetcode/issues/7
类似题目:
参考资料:
https://leetcode.com/problems/reverse-integer/
https://leetcode.com/problems/reverse-integer/discuss/4060/My-accepted-15-lines-of-code-for-Java
https://leetcode.com/problems/reverse-integer/discuss/4056/Very-Short-(7-lines)-and-Elegant-Solution
[LeetCode] Reverse Integer 翻转整数的更多相关文章
- [LintCode] Reverse Integer 翻转整数
Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer). ...
- [LeetCode] 7. Reverse Integer 翻转整数
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
- [Leetcode] reverse integer 反转整数
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to ...
- [LeetCode] Reverse Pairs 翻转对
Reverse Pairs 翻转对 题意 计算数组里面下标i小于j,但是i的值要大于j的值的两倍的搭配的个数(也就是可能会有多种搭配):网址 做法 这道题显然是不允许使用最简单的方法:两次循环,逐次进 ...
- LeetCode: Reverse Integer 解题报告
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
- [leetcode]7. Reverse Integer反转整数
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
- LeetCode——Reverse Integer(逆置一个整数)
问题: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return –321 Ha ...
- 【LeetCode】7、Reverse Integer(整数反转)
题目等级:Easy 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 O ...
- leetcode 7 reverse integer 反转整数
描述: 给定32位整数,反转,如321转成123. 解决: 关键是溢出检测: int reverse(int x) { ; int temp; while (x) { temp = ret * + x ...
随机推荐
- CE修改器修改DNF 测试视频 阿修罗提升智力增加攻击力
使用CE修改器来修改网络游戏,如DNF 测试视频: CE修改器:指的是Cheat Engine,字面上的意思指的是作弊引擎的意思,是一款内存修改编辑工具.通过修改游戏的内存数据来得到一些原本无法实现的 ...
- ASP.NET Core 中文文档 第二章 指南(4.1)ASP.NET Core MVC 与 Visual Studio 入门
原文:Getting started with ASP.NET Core MVC and Visual Studio 作者:Rick Anderson 翻译:娄宇(Lyrics) 校对:刘怡(Alex ...
- ASP.NET MVC5中的Model验证
Model验证是ASP.NET MVC中的重要部分,它主要用于判断输入的数据类型及值是否符合我们设定的规则,这篇文章就介绍下ASP.NET MVC中Model验证的几种方式. 后台验证 DataAnn ...
- how to use panda
0.Introduce pandas.read_csv(filepath_or_buffer,sep=', ', dialect=None, compression='infer', doublequ ...
- JDBC_part4_大对象_DAO_Bean_DButi
本文为博主辛苦总结,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用. 转载请注明 出自 : luogg的博客园 谢谢配合! JDBCday04_大对象_Dao_DBUtil_Ja ...
- yii框架安装心得
最近在学习yii框架, 现在将遇到的一些问题和解决方法写出来与大家分享. yii框架的安装: 下载yii框架之后, 打开文件运行init.bat文件, 如果闪退就打开php的扩展(php_openss ...
- php mail 函数发送邮件
当然你可以通过php ,在自己的站点制作一个反馈表单, 我这次的需求是用email 的形式发送数据报表,结构比较简单 // 收件人地址(此处只可以写一个地址,写多个地址,只有最后一个地址生效) $to ...
- 关于i++引出的线程不安全性的分析以及解决措施
Q:i++是线程安全的吗? A:如果是局部变量,那么i++是线程安全. 如果是全局变量,那么i++不是线程安全的. 理由:如果是局部变量,那么i++是线程安全:局部变量其他线程访问不到,所以根本不存在 ...
- 新手入门Underscore.js 中文(template)
Underscore.js是一个很精干的库,压缩后只有4KB.它提供了几十种函数式编程的方法,弥补了标准库的不足,大大方便了javaScript的编程.MVC框架Backbone.js就将这个库作为自 ...
- iscroll5实现一个下拉刷新上拉加载的效果
直接上代码!!! <!DOCTYPE html><html><head lang="en"> <meta charset="UT ...