[Leetcode] reverse integer 反转整数
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
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?
Throw an exception? Good, but what if throwing an exception is not an option? You would then have to re-design the function (ie, add an extra parameter).
题意:将一个整数反转。
思路:这题有几个要注意的地方:一、正负号;二、反转后的结果是否溢出。第二点很重要,关于这点题目中有详细的说明。这里有两种解法方法:
方法一:改变返回结果的变量类型为long long ,这样,若是最终计算的结果超过INT_MAX,就返回0;否则,结构符号输出即可。
代码如下:
class Solution {
public:
int reverse(int x)
{
long long res=;
int cur=abs(x);
while(cur>)
{
res=res*+cur%;
cur/=;
}
if(res>INT_MAX) return ;
return x>=?res:-res;
}
};
方法二:不用改变返回的变量类型,在while循环中,在计算res之前,若res>INT_MAX/10,就返回0,因为若是大于,则后续的计算中乘以10 了,还是会大于,所以提前返回。代码如下:
class Solution {
public:
int reverse(int x)
{
int res=;
int cur=abs(x);
while(cur>)
{
if(res>INT_MAX/) return ;
res=res*+cur%;
cur/=;
}
return x>=?res:-res;
}
};
在LeetCode上测试时,第二种方法,明显好些。
[Leetcode] reverse integer 反转整数的更多相关文章
- [leetcode]7. Reverse Integer反转整数
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
- leetcode 7 reverse integer 反转整数
描述: 给定32位整数,反转,如321转成123. 解决: 关键是溢出检测: int reverse(int x) { ; int temp; while (x) { temp = ret * + x ...
- [LeetCode] Reverse Integer 翻转整数
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to ...
- 7. Reverse Integer 反转整数
[抄题]: 将一个整数中的数字进行颠倒,当颠倒后的整数溢出时,返回 0 (标记为 32 位整数). 样例 给定 x = 123,返回 321 给定 x = -123,返回 -321 [暴力解法]: ...
- [LintCode] Reverse Integer 翻转整数
Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer). ...
- Reverse Integer - 反转一个int,溢出时返回0
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
- LeetCode: Reverse Integer 解题报告
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
- 【LeetCode】7、Reverse Integer(整数反转)
题目等级:Easy 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 O ...
- 7. Reverse Integer[E]整数反转
题目 Given a 32-bit signed integer, reverse digits of an integer. Example1: x = 123, return 321 Exampl ...
随机推荐
- [转帖]localhost与127.0.0.1的区别
localhost与127.0.0.1的区别 https://www.cnblogs.com/hqbhonker/p/3449975.html 前段时间用PG的时候总有问题 当时没有考虑 localh ...
- C# 简单工厂
如下: public static IList<T> Create<T>(Type type) { if (type == typeof(List<T>)) { r ...
- Jmeter断言、参数化及集合点
JMeter---QPS(Query Per Second) QPS为每秒查询率.是一台查询服务器每秒能够处理的查询次数,在因特网上,作为域名系统服务器的性能经常用每秒查询率来衡量.步骤:1.添加线程 ...
- 使用.net 更新word目录
方案一.采用OpenXml(服务器不依赖Office组件) 在word生成的最后加上代码: using (WordprocessingDocument docx = WordprocessingDoc ...
- Python简要标准库(1)
sys sys这个模块让你能够访问与Python解释器联系紧密的变量和函数 其中的一些在下表 F argv 命令行参数,包括脚本名称 exit([arg]) 退出当前的程序,可选参数为给定的返回值或者 ...
- leetcode-二进制手表
二进制手表顶部有 4 个 LED 代表小时(0-11),底部的 6 个 LED 代表分钟(0-59). 每个 LED 代表一个 0 或 1,最低位在右侧. 例如,上面的二进制手表读取 “3:25”. ...
- Apache--Override参数详解
1 AuthConfig 允许使用所有的权限指令,他们包括AuthDBMGroupFile AuthDBMUserFile AuthGroupFile AuthName AuthTypeAut ...
- LeetCode 102 ——二叉树的层次遍历
1. 题目 2. 解答 定义一个存放树中数据的向量 data,一个存放树的每一层数据的向量 level_data 和一个存放每一层节点的队列 node_queue. 如果根节点非空,根节点进队,然后循 ...
- IntelliJ IDEA for MAC 注释模板、快捷键生成注释
增加注释 在IntelliJ IDEA中为JAVA代码增加注释,首先需要配置注释模板,而后使用模板快捷键生成注释, 下面按照[配置模板].[模板使用]两部分进行介绍 ----------------- ...
- Linux命令详解----ln
ln命令 ln命令为文件或文件夹创建连接,连接类型有硬链接和符号连接两种,符号连接需要使用"-s"选项 ln语法 ln [选项] 参数 使用 ln --help查看可用选项 [ro ...