《LeetBook》leetcode题解(7): Reverse Integer[E]——处理溢出的技巧
我现在在做一个叫《leetbook》的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看
书的地址:https://hk029.gitbooks.io/leetbook/
007. Reverse Integer[E]——处理溢出的技巧
题目
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
思路
这题完全没丝毫的难度,任何人几分钟都可以写出来,但是,这题修改后,加入了一个新的测试,瞬间大家讨论的就多了,就是——溢出测试
因为整数只有32位,可能原数不会溢出,但是转置后就不一定了,所以必须要考虑溢出的情况。
思路1——用long
一个比较讨巧的方案,直接用long不会溢出再和INT_MAX比较就好了
class Solution {
public:
int reverse(int x) {
long tmp=0;
while(x != 0)
{
tmp *=10;
tmp += x%10;
if(tmp > INT_MAX || tmp < INT_MIN)
return 0;
x /= 10;
}
return tmp;
}
};
思路2——变化前后对比
bitzhuwei的代码。
不用任何flag和INT_MAX宏或者任何硬编码Ox7fffffff
这个思路 也是很容易理解的,做一些操作,如果溢出了,那溢出后的值做反向操作会和之前的值不一样。
这里就用一个变量存储变化后的值,每次做反向操作,如果和之前的值一样就更新,不一样,说明溢出了。
public int reverse(int x)
{
int result = 0;
while (x != 0)
{
int tail = x % 10;
int newResult = result * 10 + tail;
if ((newResult - tail) / 10 != result)
{ return 0; }
result = newResult;
x = x / 10;
}
return result;
}
思路3——提前停止操作
如果当前的数已经>INT_MAX/10 那么再做一次操作,必然溢出。
class Solution
{
public:
int reverse(int n)
{
int result = 0;
while (n != 0)
{
if (result > INT_MAX / 10
|| ((result == INT_MAX / 10) && (n % 10 > INT_MAX % 10)))
{
result = 0;
break;
}
if (result < INT_MIN / 10
|| ((result == INT_MIN/ 10) && (n % 10 < INT_MIN % 10)))
{
result = 0;
break;
}
result = result * 10 + n % 10;
n = n / 10;
}
return result;
}
};
《LeetBook》leetcode题解(7): Reverse Integer[E]——处理溢出的技巧的更多相关文章
- leetcode题解 7.Reverse Integer
题目: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 E ...
- C# 写 LeetCode easy #7 Reverse Integer
7.Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 ...
- Leetcode练习题 7. Reverse Integer
7. Reverse Integer 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Inp ...
- 【LeetCode】【Python题解】Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to ...
- 【LeetCode】7. Reverse Integer 整数反转
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:整数,反转,题解,Leetcode, 力扣,Python, ...
- 【一天一道LeetCode】#7. Reverse Integer
一天一道LeetCode系列 (一)题目 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, ...
- 【算法】LeetCode算法题-Reverse Integer
这是悦乐书的第143次更新,第145篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第2题(顺位题号是7),给定32位有符号整数,然后将其反转输出.例如: 输入: 123 ...
- 【LeetCode】007. Reverse Integer
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
- leetcode:7. Reverse Integer
这题简单,也花了我好长时间,我自己写的code比较麻烦,也没啥技巧:按正负性分类执行,先转化成字符串,用stringbuilder进行旋转,如果超出范围了就用try catch public int ...
随机推荐
- Sophus VS2010编译不支持?C++11语法的缘故。那有没有不带C++11特性的Sophus版本呢?
Eigen:3.1 3.0 Ceres:No Sophus: Sophus支不支持Windows编译?官网写的是通过了Windows的编译的 linux, os x: windows: code ...
- win32多线程-异步(asynchronous) I/O
I/O设备是个慢速设备,无论打印机.调制解调器,甚至硬盘,与CPU相比都奇慢无比,坐下来干等I/O的完成是一件不甚明智事情. 异步(asynchronous) I/O在win32多线程程序设计中被称为 ...
- Oracle EBS Color 色彩设置
Oracle EBS配色方案的截图 If the Java Look and Feel profile option is set to Oracle, the Java Color Scheme c ...
- Android-MediaRecorder录像机(视频)
在上一篇博客,Android-MediaRecorder录制音频,中讲解了使用Android API MediaRecorder 刻录音频,这篇博客主要是介绍 使用MediaRecorder刻录(视频 ...
- CDI Event解析
CDI(Contexts And Dependency Injection)是JavaEE 6标准中一个规范,将依赖注入IOC/DI上升到容器级别, 它提供了Java EE平台上服务注入的组件管理核心 ...
- oracle 11gr2 2.04 em 更改 hostname 后无需重建资料库的方法
1) 备份删除$ORACKE_HOME/ xxxx-sid 的EM目录:复制要创建的xxx-sid EM 名称目录: 备份删除$ORACKE_HOME/oc4j/j2ee/ xxxx-sid 的EM目 ...
- (zxing.net)一维码ITF的简介、实现与解码
一.简介 一维码ITF 25又称交插25条码,常用在序号,外箱编号等应用.交插25码是一种条和空都表示信息的条码,交插25码有两种单元宽度,每一个条码字符由五个单元组成,其中二个宽单元,三个窄单元.在 ...
- c#设计模式之观察者模式(Observer Pattern)
场景出发 一个月高风黑的晚上,突然传来了尖锐的猫叫,宁静被彻底打破,狗开始吠了,大人醒了,婴儿哭了,小偷跑了 这个过程,如果用面向对象语言来描述,简单莫过于下: public class Cat { ...
- 毕业回馈—89C51之GPIO使用
STC89C51系列单片机共有如下几类GPIO口: (1)P0.0-P0.7: 对应DIP40封装的39-32号引脚:P0口既可以作为输入/输出GPIO口,也可以作为地址/数据复用总线使用. a)P0 ...
- c# in out ref关键字
class in_out_ref { #region in 关键字 delegate void DContravariant<in A>(A argumen); static void o ...