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. 值得关注的10个Python语言学习博客

    大家好,还记得我当时学习python的时候,我一直努力地寻找关于python的博客,但我发现它们的数量很少.这也是我建立这个博客的原因,向大家分享我自己学到的新知识.今天我向大家推荐10个值得我们关注 ...

  2. 面向服务体系架构(SOA)和数据仓库(DW)的思考

    摘要: 当前业界对面向服务体系架构(SOA)和数据仓库(Data Warehouse,DW)都介绍的很多,提出了很多优秀的解决方案,但是一般是把 SOA 和 DW 单独考虑,SOA 和 DW 有着共同 ...

  3. 【HackerRank】QuickSort(稳定快排,空间复杂度O(n))

    QuickSort In the previous challenge, you wrote a partition method to split an array into 2 sub-array ...

  4. 【leetcode刷题笔记】Median of Two Sorted Arrays

    There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...

  5. ubuntu没有声音解决办法

    cd /usr/lib/dbus-1.0/ chmod +x dbus-daemon-launch-helper sudo gpasswd -a $USER audio sudo killall pu ...

  6. imx6q 添加intel PCIE网卡

    TQ_IMX6Q开发板移植rtl8168-PCIE转千兆网卡 一.配置内核选项PCIE总线驱动支持 默认的内核配置可能没有把PCIE的总线驱动编入内核,所以需要确认是否把驱动编译到了内核里面. 配置好 ...

  7. 仿Facebook登录表单

    在线演示 本地下载

  8. HISAT2的运用

    功能: 用于有参考基因组存在的比对工具(适用于whole-genome, transcriptome, and exome sequencing data) 用法: hisat2-build [opt ...

  9. JSP Cookie状态管理

    JSP中创建与使用Cookie 创建Cookie对象 Cookie newCookie = new Cookie(String key, Object value); 写入Cookie对象 respo ...

  10. HMM简单理解(来自quora&其他网上资料)

    转载自quora: 连接:https://www.quora.com/What-is-a-simple-explanation-of-the-Hidden-Markov-Model-algorithm ...