7. Reverse Integer
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的更多相关文章
- Python字符串倒序-7. Reverse Integer
今天做了下LeetCode上面字符串倒序的题目,突然想Python中字符串倒序都有哪些方法,于是网上查了下,居然有这么多种方法: 个人觉得,第二种方法是最容易想到的,因为List中的reverse方法 ...
- [LintCode] Reverse Integer 翻转整数
Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer). ...
- 65. Reverse Integer && Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, re ...
- LeetCode 7 Reverse Integer(反转数字)
题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...
- No.007 Reverse Integer
7. Reverse Integer Total Accepted: 153147 Total Submissions: 644103 Difficulty: Easy Reverse digits ...
- leetcode第七题Reverse Integer (java)
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, retu ...
- Reverse Integer 2015年6月23日
题目: Reverse digits of an integer. Example1: x = , return Example2: x = -, return - 思路:递归 解答: / test ...
- Reverse Integer - 反转一个int,溢出时返回0
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
- LeetCode之Easy篇 ——(7)Reverse Integer
7.Reverse Integer Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: Out ...
- LeetCode之“数学”:Reverse Integer && Reverse Bits
1. Reverse Integer 题目链接 题目要求: Reverse digits of an integer. Example1: x = 123, return 321 Example2: ...
随机推荐
- perl 一个简单的面向对象的例子
<pre name="code" class="python">[root@wx03 wx]# cat x1.pm package x1; use ...
- CC++初学者编程教程(13) 基于Oracle linux 的Oracle12c环境搭建
1设置虚拟机选项 2 设置文件夹共享 3启动文件夹共享向导 4 设置共享文件夹 5 启用共享 6 关闭虚拟机设置 7 开启虚拟机 8 登陆帐户 9 看见虚拟机桌面 10 安装vmwaretools 1 ...
- 弗洛伊德(Floyd)算法
#include <stdio.h> #define MAXVEX 20 //最大顶点数 #define INFINITY 65535 //∞ typedef struct {/* 图结构 ...
- Qt部件学习之-烧鹅
1,布局管理器 candidates
- Xcode7.3.1真机调试ios10
如果自己的ios测试机不小心升级到比Xcode更高的ios系统, 那么这时候是无法使用真机来进行调试的. 但是我们可以通过拷贝与测试机一样版本的系统来解决这个问题. 去下载一个Xcode8,然后安装, ...
- JavaScript-1.最简单的程序之网页弹出对话框,显示为Warning---ShinePans
代码: <html> <head> <meta http-equiv="content-type" content="text/html;c ...
- 垃圾回收算法简单介绍——JVM读书笔记<二>
垃圾回收的过程主要包含两部分:找出已死去的对象.移除已死去的对象. 确定哪些对象存活有两种方式:引用计数算法.可达性分析算法. 方案一:引用计数算法 给对象中加入一个引用计数器.每当有一个地方引用它时 ...
- Acess错误:"文件共享锁定数溢出"
对于ACCESS数据库,如果通过大量的SQL来操作数据库或者直接操作大量的数据时,经常会出现这种错误: "文件共享锁定数溢出" 原因如下: Access数据库,同时操作大量 ...
- 关闭浏览器输入框自动补齐 兼容IE,FF,Chrome等主流浏览器
这篇文章主要介绍了关闭浏览器输入框自动补齐 兼容IE,FF,Chrome等主流浏览器,需要的朋友可以参考下.希望对大家有所帮助 Firefox 和 IE 的浏览器各自实现了input历史记录的功能 ...
- zen-Coding的使用
zen-Coding的使用 zen-Coding的使用需要掌握CSS和HTML相关知识.其实只要对CSS的选择器比较熟悉,就可以得用简短的类似于CSS选择器的代码高效的编写出HTML代码.打开Note ...