[LeetCode] 7. Reverse Integer 翻转整数
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 store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
解法一:
class Solution {
public:
int reverse(int x) {
int res = ;
while (x != ) {
if (abs(res) > INT_MAX / ) return ;
res = res * + x % ;
x /= ;
}
return res;
}
};
在贴出答案的同时,OJ 还提了一个问题 To check for overflow/underflow, we could check if ret > 214748364 or ret < –214748364 before multiplying by 10. On the other hand, we do not need to check if ret == 214748364, why? (214748364 即为 INT_MAX / )
为什么不用 check 是否等于 214748364 呢,因为输入的x也是一个整型数,所以x的范围也应该在 -2147483648~2147483647 之间,那么x的第一位只能是1或者2,翻转之后 res 的最后一位只能是1或2,所以 res 只能是 2147483641 或 2147483642 都在 int 的范围内。但是它们对应的x为 1463847412 和 2463847412,后者超出了数值范围。所以当过程中 res 等于 214748364 时, 输入的x只能为 1463847412, 翻转后的结果为 2147483641,都在正确的范围内,所以不用 check。
我们也可以用 long 型变量保存计算结果,最后返回的时候判断是否在 int 返回内,但其实题目中说了只能存整型的变量,所以这种方法就只能当个思路扩展了,参见代码如下:
解法二:
class Solution {
public:
int reverse(int x) {
long res = ;
while (x != ) {
res = * res + x % ;
x /= ;
}
return (res > INT_MAX || res < INT_MIN) ? : res;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/7
类似题目:
参考资料:
https://leetcode.com/problems/reverse-integer/
https://leetcode.com/problems/reverse-integer/discuss/4060/My-accepted-15-lines-of-code-for-Java
https://leetcode.com/problems/reverse-integer/discuss/4056/Very-Short-(7-lines)-and-Elegant-Solution
[LeetCode] 7. Reverse Integer 翻转整数的更多相关文章
- [LintCode] Reverse Integer 翻转整数
Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-bit integer). ...
- [LeetCode] Reverse Integer 翻转整数
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to ...
- [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 7. Reverse Integer 一个整数倒叙输出
潜在问题:(1)随着求和可能精度会溢出int 范围,需要使用long 来辅助判断是否溢出,此时返回 0 Assume we are dealing with an environment which ...
- [LeetCode] 190. Reverse Bits 翻转二进制位
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- LeetCode 7 Reverse Integer(反转数字)
题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...
- [LeetCode] 493. Reverse Pairs 翻转对
Given an array nums, we call (i, j) an important reverse pair if i < j and nums[i] > 2*nums[j] ...
- leetcode:Reverse Integer 及Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
随机推荐
- leetcode 410. 分割数组的最大值(二分法)
1. 题目描述 给定一个非负整数数组和一个整数 m,你需要将这个数组分成 m 个非空的连续子数组.设计一个算法使得这 m 个子数组各自和的最大值最小. 注意: 数组长度 n 满足以下条件: 1 ≤ n ...
- ubuntu python 版本管理
ubuntu 命令行查看 python 目录 $ whereis python # 显示所有得到 python 目录 $ which python # 显示默认的 python 解释器目录 $ wh ...
- PDF文件添加二维码水印教程
maven配置iText的jar,主要不是所有私服都有iText的jar,maven仓库没有的,可以去https://mvnrepository.com/artifact/com.itextpdf/i ...
- 容器网络插件那么多,博云为什么基于OVS深度自研?
背景 从2015年开始,博云开始基于Kubernetes和容器帮助客户交付应用管理平台.在开始阶段,博云选择了业界使用度非常广泛且成熟稳定的calico作为默认的网络方案并在calico方面积累了大量 ...
- 修改linux内核加载顺序
修改内核启动顺序:1.查看当前系统所有的内核# awk -F\' '$1=="menuentry " {print i++ " : " $2}' /etc/gr ...
- GAC 解释&路径
GAC 全称是 Global Assembly Cache 作用是可以存放一些有很多程序都要用到的公共 Assembly ,例如 System.Data .System.Windows.Form 等等 ...
- Anchor 和 Dock 属性的使用
Anchor 是一个常用属性,用来控制当窗体大小变化,控件如何自动调整自身大小和位置 一 仅设置一个值 如果此时将窗体放大,将会变成这样: 由于固定了top, 所以top不变,那么bottom自然会因 ...
- Java操作zip-压缩和解压文件
一.说明 rar格式的压缩包收费,java支持zip格式的压缩和解压 二.工具类 import java.io.*; import java.util.Enumeration; import java ...
- OPC 集成的五大要素,你都掌握了吗?
相信在处理工业项目集成问题的时候,自动化集成供应商真正需要的不是那些华丽的宣传语,而是提供真正的通信数据集成实力. 任何自动化集成的供应商都希望能够消除中间的层层障碍,从而实现真正的信息集成互通.那么 ...
- 数据挖掘--DBSCAN
DBSCAN:Density Based Spatial Clustering of Applications with Noise Basic idea: If an object p is den ...