《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 ...
随机推荐
- Python入门基础学习 一
Python入门基础学习 一 Python下载及安装 下载地址:https://www.python.org/,选择最新的版本下载 稍等一会,安装完成. 简单语句 从idle启动Python:IDLE ...
- vs 你不得不会的调试方式
常规调试F5 一般情况下,我们在使用vs的jdk调试程序,通常是使用F5这种常规编译方式,很方便 but,编译的速度是so慢,慢的让人无法忍受,通常一个稍大一点的项目跑起来就需要一分钟,甚至两分钟,作 ...
- 和我一起学python
近来python越来越火,很多人都出了教程,我也来出一个凑凑热闹吧. pycharm激活 http://idea.lanyus.com/ https://blog.csdn.net/u01404481 ...
- NOIP2015BLOCKADE c++ 代码
#include<algorithm> #include<fstream> #include<functional> #include<iostream> ...
- 【12c OCP】CUUG OCP认证071考试原题解析(33)
33.choose the best answer View the Exhibit and examine the structure of the ORDER_ITEMS table. Exami ...
- linux强制安装rpm包的命令
rpm -ivh *********.rpm --nodeps --force 强制安装会忽略掉所有依赖关系,强制进行安装
- 虚拟机搭建ftp环境
引用http://www.cnblogs.com/xiangxiaodong/archive/2013/12/23/3487028.html,学习. 本人是在windows8系统下,Oracle VM ...
- Linux 解压 压缩文件
来源于:http://blog.csdn.net/mmllkkjj/article/details/6768294/ 解压 tar –xvf file.tar //解压 tar包tar -xzvf f ...
- Haproxy搭建Web群集
一.Haproxy与LVS LVS不支持正则处理,不能实现动静分离,对于大型网站,LVS的实施配置复杂,维护成本相对较高 Harpoxy是一款可提供高可用性,负载均衡.及基于TCP和HTTP应用的代理 ...
- Microsoft Visual C++ 14.0 is required
building 'twisted.test.raiser' extensionerror: Microsoft Visual C++ 14.0 is required. Get it with &q ...