1. 问题描述

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

click to show spoilers.

Have you thought about this?
Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!
If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.
Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?
For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

Solution.

class Solution {
public:
int reverse(int x){
}
};

2. 解答思路

2.1. 为什么会有溢出?
整数的存储方式:最高位为符号位

  • 16位整数范围:-2^15~2^15-1,即-32768到32767
  • 32位整数范围:-2^31~2^31-1,即-2147483648到2147483647

2.2. 如何判断溢出?

  • 传入的整数x的范围是:-2147483648 到 2147483648,反转后(暂不考虑溢出问题):-2147483648 --> -8463847412; 2147483647 -->7463847412。
  • 由32位整数的范围知,当且仅当整数x是10位数时,才可能溢出。
  • 当x是10位数时,考虑个位,若x%10 > 2,则溢出;若x%10 < 2,则未溢出;若x%10 == 2,则需考虑x的十位。若十位为1,则需考虑百位,以此类推..

3. 代码

 class Solution {
public:
int reverse(int x){
bool bIsPositive = x > ;
x = bIsPositive ? x : -x;//也可调用求绝对值的函数abs(x) if (IsOverFlow(x))//处理溢出
{
return ;
} int result = ;
while ( != x)
{
result = result * + x%;
x = x/;
}
if (!bIsPositive)
{
result = -result;
} return result;
}
private:
/*! \fn bool IsOverFlow(int x)
* \brief 判断输入的整数x是否溢出.
* \param[in] x 用户输入的32位整数.
* \return 结果.
* - \b true 溢出.
* - \b false 未溢出.
*/
bool IsOverFlow(int x)
{
if (- == x)//注意,-2147483648的绝对值溢出,-x仍未-2147483648,故需单独判断。
{
return true;
} int nBitCount = ;//记录当前输入整数的位数
int tx = x;
while ( != tx)
{
nBitCount++;
tx = tx/;
}
if ( == nBitCount)//2147483647 1463847412
{
if (recIsOverFlow(x))
{
return true;
}
} return false;
}
/*! \fn bool recIsOverFlow(int x, int idx = 1463847412)
* \brief 递归判断输入的整数x是否溢出.
* \param[in] x 用户输入的32位整数.
* \param[in] idx 用于判断溢出的整数.
* \return 结果.
* - \b true 溢出.
* - \b false 未溢出.
*/
bool recIsOverFlow(int x, int idx = )
{
int x_remainder = x % ;
int idx_remainder = idx % ; if (x_remainder > idx_remainder)
{
return true;
}
else if (x_remainder == idx_remainder && x!= && idx!=)
{
return recIsOverFlow(x/, idx/);
}
return false;
}
};

4. 反思

对于-2147483648,由于其绝对值溢出,-x仍未-2147483648,故需单独判断。

7. Reverse Integer的更多相关文章

  1. Python字符串倒序-7. Reverse Integer

    今天做了下LeetCode上面字符串倒序的题目,突然想Python中字符串倒序都有哪些方法,于是网上查了下,居然有这么多种方法: 个人觉得,第二种方法是最容易想到的,因为List中的reverse方法 ...

  2. [LintCode] Reverse Integer 翻转整数

    Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer). ...

  3. 65. Reverse Integer && Palindrome Number

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

  4. LeetCode 7 Reverse Integer(反转数字)

    题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...

  5. No.007 Reverse Integer

    7. Reverse Integer Total Accepted: 153147 Total Submissions: 644103 Difficulty: Easy Reverse digits ...

  6. leetcode第七题Reverse Integer (java)

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

  7. Reverse Integer 2015年6月23日

    题目: Reverse digits of an integer. Example1: x = , return Example2: x = -, return - 思路:递归 解答: / test ...

  8. Reverse Integer - 反转一个int,溢出时返回0

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

  9. LeetCode之Easy篇 ——(7)Reverse Integer

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

  10. LeetCode之“数学”:Reverse Integer && Reverse Bits

    1. Reverse Integer 题目链接 题目要求: Reverse digits of an integer. Example1: x = 123, return 321 Example2:  ...

随机推荐

  1. 迁移到MSYS2 与 Qt 工具链注意的几个事情(注意链接顺序,并且人造mingw工具链所没有的局部midl.exe命令)

    Microsoft Visual Studio 2015社区版提供了强大的开发体验,且 Qt 提供了预编译版本.然而,由于客户提出兼容Windows XP ~ Windows 8.1 这样宽泛的环境要 ...

  2. oracle的常见问题与解决

    刚接触oracle,在学习过程中遇到了很多的问题,本文章将会收藏我遇到的问题及如何解决. 错误一:ORA-28009:connection as sys should be as sysdba解决方法 ...

  3. uva 10306 - e-Coins(完全背包)

    题目链接:10306 - e-Coins 题目大意:给出m和s, 再给出m种电子硬币,每种硬币有两种金额xi,yi.现在要在m种硬币种选若干个硬币,可以重复选同一种硬币, 使得(x1 + x2 + . ...

  4. POJ2528线段树基础

    開始就直接用延迟标记搞了下.最后发现内存肯定会爆了.数据太大了. 问了瓜神,原来应该用离散化来做这题,详细见凝视 #include <cstdio> #include <cstrin ...

  5. node.js(四)path优化(路径优化)

    1.normalize函数的基本用法 normalize函数将不符合规范的路径经过格式化转换为标准路径,解析路径中的.与..外,还能去掉多余的斜杠. 如下示例: var path = require( ...

  6. jquery 浏览器放大缩小函数resize

    <script> $(function(){ $(window).resize(function(){ var _height = $(window).height(); var _con ...

  7. Linux 环境变量和source命令 (转)

    可能是班门弄斧了,仅share给尚不知道的童鞋. 1.       问题的来源: 为什么我们编译Android代码时,需要输入:  source ./build/envsetup.sh  或者 . . ...

  8. EassyMock实践 自定义参数匹配器

    虽然easymock中提供了大量的方法来进行参数匹配,但是对于一些特殊场合比如参数是复杂对象而又不能简单的通过equals()方法来比较,这些现有的参数匹配器就无能为力了.easymock为此提供了I ...

  9. C++ 获取UUID

    #include <string> #include <stdio.h> #if defined(WIN32)||defined(WINCE)||defined(WIN64) ...

  10. 纯CSS 贴底部的布局(兼容各个浏览器包括IE6)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...