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 hold integers within the 32-bit signed integer range. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

题解:

   难点在于溢出的判断上。当 abs(res) 大于等于 INT_MAX / 10 时,就需要判断结果是否要溢出了。INT_MIN = -2147483648,INT_MAX = 2147483647。这里为什么不需要在

 class Solution {
public:
int reverse(int x) {
int res = ;
while (x) {
int digit = x % ;
if (abs(res) > INT_MAX / || res == INT_MAX / && digit > || res == INT_MIN / && digit < -)
return ;
res = res * + digit;
x /= ;
} return res;
}
};

    其实在 abs(res) == INT_MAX / 10 时可以直接做溢出处理,原因是输入 x 是 int 类型,所以 x 的范围也应该在 INT_MIN 和 INT_MAX 之间,那么 x 的第一位只能是1或者2,x 翻转之后 res 的最后一位只能是1或2。当 abs(res) == INT_MAX / 10时,下一步的 res 只能是 (-)2147483641 或 (-)2147483642,对应的 x 为 1463847412 和 2463847412,而2463847412 超出了 int 的数值范围。所以当 res 等于 214748364 时, 输入的 x 只能为 1463847412, 翻转后的结果 res 为 2147483641,在正确的范围内。

  

 class Solution {
public:
int reverse(int x) {
int res = ;
while (x) {
int digit = x % ;
if (abs(res) > INT_MAX / )
return ;
res = res * + digit;
x /= ;
} return res;
}
};

转自:Grandyang

【LeetCode】007. Reverse Integer的更多相关文章

  1. 【LeetCode】#7 Reverse Integer

    [Question] Reverse digits of an integer. Example: x = 123, return 321 x = -123, return -321 [My Solu ...

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

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

  3. 【LeetCode】7. Reverse Integer 整型数反转

    题目: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 思路:不 ...

  4. 【leetcode】7. Reverse Integer

    题目描述: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 解题思 ...

  5. 【leetcode】557. Reverse Words in a String III

    Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...

  6. 【LeetCode】151. Reverse Words in a String

    Difficulty: Medium  More:[目录]LeetCode Java实现 Description Given an input string, reverse the string w ...

  7. 【LeetCode】String to Integer (atoi) 解题报告

    这道题在LeetCode OJ上难道属于Easy.可是通过率却比較低,究其原因是须要考虑的情况比較低,非常少有人一遍过吧. [题目] Implement atoi to convert a strin ...

  8. 【LeetCode】#344 Reverse String

    [Question] Write a function that takes a string as input and returns the string reversed. Example: G ...

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

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

随机推荐

  1. $《利用Python进行数据分析》学习笔记系列——IPython

    本文主要介绍IPython这样一个交互工具的基本用法. 1. 简介 IPython是<利用Python进行数据分析>一书中主要用到的Python开发环境,简单来说是对原生python交互环 ...

  2. JS字符串数组转换

    字符串转数组: str.split(';') 数组转字符串: arr.join(';')

  3. 收集整理的awk用法小结

    awk 用法:awk ‘ pattern {action} ‘ 变量名 含义 ARGC 命令行变元个数 ARGV 命令行变元数组 FILENAME 当前输入文件名 FNR 当前文件中的记录号 FS 输 ...

  4. 嵌入式Qt程序启动参数-qws 不需要X11桌面系统

    1 背景 通过串口终端启动arm开发板(linux系统)的Qt应用程序,提示: [root@FORLINX6410]# /opt/qt-4.7.1/demos/textedit/textedit s3 ...

  5. 《深度学习框架PyTorch:入门与实践》的Loss函数构建代码运行问题

    在学习陈云的教程<深度学习框架PyTorch:入门与实践>的损失函数构建时代码如下: 可我运行如下代码: output = net(input) target = Variable(t.a ...

  6. Kubernetes busybox nslookup问题

    使用最新版本的busybox会出现nslookup提示无法解析的问题: Server: 10.96.0.10 Address: 10.96.0.10:53 ** server can't find k ...

  7. nginx常见面试题1

    Nginx是网页服务器运维人员不可能绕开的一个弯,剩下几个比较高危的面试范围是:linux基础.网络知识基础.python,或许还会有zabbix等监控工具.这里先说nginx,后面几个肯定也会写. ...

  8. 初识Spring security-无Security的SpringMVC

    百度百科定义: Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架.它提供了一组可以在Spring应用上下文中配置的Bean,充分利用了 ...

  9. centos下安装python2.7.9和pip以及数据科学常用的包

    以前一直用ubantu下的python,ubantu比较卡.自己倾向于使用centos,但默认的python版本太低,所以重新装了一个python和ipython centos6.5安装python2 ...

  10. 介绍一下Hibernate的二级缓存

    介绍一下Hibernate的二级缓存 按照以下思路来回答:(1)首先说清楚什么是缓存,(2)再说有了hibernate的Session就是一级缓存,即有了一级缓存,为什么还要有二级缓存,(3)最后再说 ...