《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 ...
随机推荐
- Appium之uiautomator定位元素
元素定位方式有多种,Android也有自身独有的定位方式.下面就单独介绍其基于uiautomator定位元素的方法: 基本语法: driver.find_element_by_android_uiau ...
- Web 应用简单测试方案
测试:一定要分阶段测试,先确定入队列成功,再测试队列的执行是否成功. 功能点: 1. 翻页2. 加精3. 置顶4. 帖子浏览量(PV)5. 发帖6. 回复7. 评论 8. crontab 脚本 @20 ...
- Java enum枚举类型
java的枚举类型详解: 简单示例: public enum Color{ RED,BLUE,BLACK,YELLOW,GREEN } 复杂示例(带自定义构造方法与类型) public enum En ...
- 7.使用ZookeeperNet进行CDU操作参数详解
一.创建,删除,更新 1. zkCli.sh 中使用命令执行. 2. 学会查看api文档 zookeeper C# driver 官方文档,文档是java代码的. http://zookeeper.a ...
- Python学习-10.Python函数定义(二)
在Python中定义函数的时候,可以使用参数默认值的方式定义函数 例子: def welcome(who,state='is',action='talking'): print(who,state,a ...
- mysql查看数据库性能常用命令
mysql> show global status; 可以列出MySQL服务器运行各种状态值,另外,查询MySQL服务器配置信息语句: mysql> show variables; 一.慢 ...
- python day25 正则表达式
2019.4.30 S21 day25笔记总结 正则表达式 1. 正则表达式 re模块:re模块本身只是用来操作正则表达式的,和正则本身没关系. 正则表达式:是一种规则 匹配字符串的规则. 为什么要有 ...
- Unity3d之Coroutine
在Unity3d中使用C#时,Coroutine是一个大有用处的好东西,至于怎么用网上多的是讲,我仅在此记录最近一次使用中的小发现. 因为某种需求,要在一个Coroutine实现中使用while循环, ...
- pipeline构建时报错问题解决
问题: 1.No such field found: field java.lang.String sh. Administrators can decide whether to approve o ...
- 手动编译安装lamp之mysql
转自马哥教育的讲课文档 二.安装mysql-5.5.28 1.准备数据存放的文件系统 新建一个逻辑卷,并将其挂载至特定目录即可.这里不再给出过程. 这里假设其逻辑卷的挂载目录为/mydata,而后需要 ...