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. 苹果终端wifi图标点亮慢和portal弹窗机制分析以及处理办法和建议

    转:http://kms.h3c.com/View.aspx?id=52875 问题现象 在无线环境中经常碰到苹果终端连接无线后wifi图标无法点亮导致终端无法上网.在起portal的网络中认证页面无 ...

  2. linux 基础二---用户群租权限

    用户&群组&权限 一.用户 1.用户及passwd文件 1) 掌握/etc/passwd文件的功能:存储所有用户的相关信息,该文件也被称为用户信息数据库(Database). 2) / ...

  3. P3437 [POI2006]TET-Tetris 3D

    题目 P3437 [POI2006]TET-Tetris 3D 做法 一眼就是二维线段树,仔细想想,赋值操作怎么办??\(lazy\)标记放在一维,下一次又来放个标记二维就冲突了 正解:永久化标记 怎 ...

  4. Python 字典Dict概念和操作

    # 字典概念:无序的, 可变的键值对集合 # 定义 # 方式1 # {key: value, key: value...} # 例如 # {"name": "xin&qu ...

  5. DB2日期及时间的使用

    1.DB2中日期.时间寄存器的使用 --取日期 SELECT CURRENT DATE FROM sysibm.sysdummy1; --返回数据类型:DATE --结果集(YYYY-MM-DD): ...

  6. Linux挂载第二块硬盘操作方法

    远程SSH登录上Centos服务器后,进行如下操作 提醒:挂载操作会清空数据,请确认挂载盘无数据或者未使用 第一步:列出所有已挂载磁盘 命令: disk -h [root@gluster_node1 ...

  7. firefox和chrome实现页面打印自动分页

    在Firefox和chrome中直接调用打印功能的js方法是 window.print(); 但是如果页面很长,那么就需要分页,这时只需要在页面中添加css属性即可,如果想自动分页,则如下所示 < ...

  8. java 实现HTTP连接(HTTPClient)

    在实习中,使用到了http连接,一直理解的很模糊,特地写个分析整理篇.分析不到位的地方请多多指教. Http 目前通用版本为 http 1.1 . Http连接大致分为2种常用的请求——GET,POS ...

  9. QT 创建主窗口 MainWindow 实例

    1. 2. mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include & ...

  10. Educational Codeforces Round 15 A, B , C 暴力 , map , 二分

    A. Maximum Increase time limit per test 1 second memory limit per test 256 megabytes input standard ...