我现在在做一个叫《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]——处理溢出的技巧的更多相关文章

  1. leetcode题解 7.Reverse Integer

    题目: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 E ...

  2. C# 写 LeetCode easy #7 Reverse Integer

    7.Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 ...

  3. Leetcode练习题 7. Reverse Integer

    7. Reverse Integer 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Inp ...

  4. 【LeetCode】【Python题解】Reverse Integer

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

  5. 【LeetCode】7. Reverse Integer 整数反转

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:整数,反转,题解,Leetcode, 力扣,Python, ...

  6. 【一天一道LeetCode】#7. Reverse Integer

    一天一道LeetCode系列 (一)题目 Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, ...

  7. 【算法】LeetCode算法题-Reverse Integer

    这是悦乐书的第143次更新,第145篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第2题(顺位题号是7),给定32位有符号整数,然后将其反转输出.例如: 输入: 123 ...

  8. 【LeetCode】007. Reverse Integer

    Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...

  9. leetcode:7. Reverse Integer

    这题简单,也花了我好长时间,我自己写的code比较麻烦,也没啥技巧:按正负性分类执行,先转化成字符串,用stringbuilder进行旋转,如果超出范围了就用try catch public int ...

随机推荐

  1. 试题 G: 外卖店优先级 第十届蓝桥杯

    试题 G: 外卖店优先级时间限制: 1.0s 内存限制: 512.0MB 本题总分: 20 分[问题描述]“饱了么”外卖系统中维护着 N 家外卖店,编号 1 ∼ N.每家外卖店都有一个优先级,初始时 ...

  2. handsontable-developer guide-cell type

    单元格类型:这里有很多没见过的用法,得好好总结一下 //预定义的类型Text Numeric Checkbox Date Select Dropdown Autocomplete Password H ...

  3. 使用memset初始化C++自定义类型

    当类型本身或者类型的成员变量带有虚函数以及像std::vector这类复杂数据结构的时候.就会出错,原因是memset把类型本身所带的一些隐含的信息也给置0了.如:虚表指针.std::vector的内 ...

  4. 关于windows服务注册的问题

    开发工具:VS2012 语言:C# 今天的工作内容是把wcf服务以windows服务的方式运行,由于之前没有做过windows服务,所有在网上找了些文章来看下,发现创建windows 服务是一件很简单 ...

  5. Linux Socket - 内核非阻塞功能

    select 函数 int select(int maxfdp,fd_set *readfds,fd_set *writefds,fd_set *errorfds,struct timeval*tim ...

  6. 解决<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" 过长

    解决<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" 过长 <i ...

  7. C# 加载配置文件

    //加载配置文件 var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .Add ...

  8. nginx反向代理后abp的webapi host如何获取客户端ip?

    dotnet core 跨平台是微软伟大的创举,脱离iis后服务器成本都降低了. 问题 这不,采用abp搞了个小项目,部署到centos后发现审计日志里面的ip信息不对. 解决 这个问题在.net 4 ...

  9. JAVA—IO操作

    一.JAVA I/O 输入输出流 1:编码问题 2:File类的使用 3:RandomAccessFile的使用 4:字节流的使用 5:字符流的使用 6:对象的序列化和反序列化 2: file类的使用 ...

  10. WPF 新手引导

    参考了https://www.cnblogs.com/ZXdeveloper/p/8391864.html,自己随便实现了一个,记录下,比较丑 <Window x:Class="Use ...