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. 《Android底层接口与驱动开发技术详解》digest

    第一章:IDE:Eclipse ADT for java developer其它: Apache Ant Java SE Development Kit5或6 Linux和Mac上使用Apache A ...

  2. 合理的keyword密度散布与黑帽SEO之躲藏文本

    合理的keyword密度散布与黑帽SEO之躲藏文本 咱们都晓得.关于baidu的keyword排行有一个非常重要的条件即是keyword密度.在咱们的了解中keyword的密度在2%-8%这个规模之内 ...

  3. SetEvent/ResetEvent

    在自己主动重置事件对象中,当WaitSingleObject/WaitForMultipleObjects接收到SetEvent发送过来的信号后则返回WAIT_OBJECT_0,此时操作系统(待定)自 ...

  4. google自定义站内搜索

    ttps://www.google.com/cse/docs/cref.html?hl=zh-cn 重要表单参数: action 字段:您希望存储结果的网址(在该例中,我们使用 http://www. ...

  5. jquery第四期:对象转换的小实例

    为了更清晰的看懂jquery对象是一个数组,我们这一期来改变每一个<li>中的值,在前面加上序号. 代码如下: <!DOCTYPE html PUBLIC "-//W3C/ ...

  6. ViewDragHelper练习使用

    转载博客地址:http://www.cnblogs.com/flyme2012/p/4076674.html 这个Demo是用来练习VIewDragHelper的,也是仿照网上一个大神的代码.我通过他 ...

  7. NHibernate 3.3

    今天试了一下NHibernate 3.3比之前的版本简单,只需要引入两个dll,这两个dll分别是:Iesi.Collections.dll和NHibernate.dll 通过 http://nhfo ...

  8. CentOS6.3下搭建vsftpd(采用虚拟用户设置)

    CentOS6.3如果在安装的时候所有安装选项都打勾的话就含有单间vsftpd必备的软件:vsftpd.pam*.db4* 检查是否安装: [root@centos6 ~]# rpm -qa | gr ...

  9. Temporary Post Used For Theme Detection (19f70e1d-5d8d-4c19-aef1-5b5a71ae0c47 - 3bfe001a-32de-4114-a6b4-4005b770f6d7)

    This is a temporary post that was not deleted. Please delete this manually. (90462755-633b-4a0e-abb7 ...

  10. IOS 获取手机各种信息

    /手机序列号      NSString* identifierNumber = [[UIDevice currentDevice] uniqueIdentifier];     NSLog(@&qu ...