【LeetCode】Reverse Integer(整数反转)
这道题是LeetCode里的第7道题。
题目描述:
给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。
示例 1:
输入: 123
输出: 321
示例 2:
输入: -123
输出: -321
示例 3:
输入: 120
输出: 21
注意:
假设我们的环境只能存储得下 32 位的有符号整数,则其数值范围为 [−231, 231 − 1]。请根据这个假设,如果反转后整数溢出那么就返回 0。
首先,这是一道简单题,根据我多年的做题经验来看,这道题肯定有坑,绝对会有 INT_MAX_VALUE 和 INT_MIN_VALUE 的样例。所以我们肯定需要判断溢出。具体的过程在代码中,
- INT_MAX_VALUE = 2^31-1 = 2147483647
- INT_MIN_VALUE = -2^31 = -2147483648
提交代码:
class Solution {
public int reverse(int x) {
int res=0;
//boolean flag=x<0?false:true;
if(x>0){
while(true){
res+=x%10;
x/=10;
if(x==0)return res;
if(res>214748364||res==214748364&&x%10>7)return 0;
res*=10;
}
}else{
while(true){
res+=x%10;
x/=10;
if(x==0)return res;
if(res<-214748364||res==-214748364&&x%10<-8)return 0;
res*=10;
}
}
}
}
才发现可以简化代码:
class Solution {
public int reverse(int x) {
int res=0;
//boolean flag=x<0?false:true;
while(true){
res+=x%10;
x/=10;
if(x==0)return res;
if(res>214748364||res==214748364&&x%10>7)return 0;
if(res<-214748364||res==-214748364&&x%10<-8)return 0;
res*=10;
}
}
}
提交结果:
个人总结:
虽然没有踩进本题的坑里,但是我被这道题秀了一波数学,负数的余数是负数。
StringBuffer 法:
class Solution {
public int reverse(int x) {
StringBuffer sb = new StringBuffer();
String str = sb.append(String.valueOf(x < 0 ? x * -1 : x)).reverse().toString();
try{
return x < 0 ? Integer.parseInt(str) * -1 : Integer.parseInt(str);
}catch(Exception e){
return 0;
}
}
}
运行时间:
其他代码:
class Solution {
public int reverse(int x) {
String s=String.valueOf(Math.abs(x));
StringBuilder sb=new StringBuilder();
for (int i=s.length()-1;i>=0;--i){
sb.append(s.charAt(i));
}
try{
return x>0?Integer.valueOf(sb.toString()):-Integer.valueOf(sb.toString());
}catch (Exception e){
return 0;
}
}
}
class Solution {
public int reverse(int x) {
char[] s = Integer.toString(x).toCharArray();
int f = 0,b = s.length-1;
String string = "";
if (s[f] == '-') {
string += '-';
++f;
}
while (b >= 0&& s[b] == '0') {
--b;
}
while (f <= b) {
string += s[b--];
}
if (string != "") {
long num = Long.valueOf(string);
if (num < (1<<31)-1 && num > -(1<<31)) {
return (int)num;
}
}
return 0;
}
}
关于溢出的问题,最方便的解决方法是当溢出时直接抛出异常,然后在异常处理语句中返回 0,当然这个方法对 JDK 的版本有要求。具体读者可以自行搜索。
【LeetCode】Reverse Integer(整数反转)的更多相关文章
- 【LeetCode】7. Reverse Integer 整数反转
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:整数,反转,题解,Leetcode, 力扣,Python, ...
- [LeetCode]7. Reverse Integer整数反转
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
- Leetcode7 : Reverse Integer 整数反转问题
问题描述 Example1: x = 123, return 321 Example2: x = -123, return -321 原题链接: https://leetcode.com/proble ...
- [Leetcode] reverse integer 反转整数
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to ...
- LeetCode 7 Reverse Integer(反转数字)
题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...
- Leetcode(力扣) 整数反转
Leetcode 7.整数反转 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例: 输入: -123 输出: -321 注意: 假设我们的环境只能存储得下 32 位的有符 ...
- LeetCode Golang 7. 整数反转
7. 整数反转 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. Tips : Math包给出的类型大小的边界: // Integer limit values. const ...
- 每日一道 LeetCode (2):整数反转
题目:整数反转 题目来源:https://leetcode-cn.com/problems/reverse-integer 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示 ...
- LeetCode: Reverse Integer 解题报告
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
随机推荐
- 编译错误you should not run configure as root (set FORCE_UNSAFE_CONFIGURE=1 in environment to bypass this check)
解决方法: export FORCE_UNSAFE_CONFIGURE=1
- Ubuntu11.04 安装cuda4.3
一.卸载官方驱动并安装显卡驱动 1. sudo gedit /etc/modprobe.d/blacklist.conf,在文件末尾加上如下五行,然后保存 blacklist vga16fb blac ...
- [选择排序] 时间复杂度O(n^2)
思路:从未排序的序列中,找到最小的元素,放到序列的起始位置, 再从剩下没排序的里面,找到最小的,放到已经排序的末尾. 原地操作几乎是选择排序的唯一优点,当空间复杂度要求较高时,可以考虑选择排序:实际适 ...
- Nginx开启Gzip压缩提高页面加载速度
本文转自http://www.veryhuo.com/a/view/51706.html,如有侵权,请及时联系转载人删除! 在实际运维中,为了提高web页面的访问加载速度,一般会把静态资源(比如js. ...
- VMware与Hyper-V不兼容
一.问题描述 VMware Workstation与Hyper-V不兼容. 二.解决方案 取消Hyper-V功能,即将Hyper-V框中钩去掉. 三.总结思考 开始不清楚怎么解决这个问题,主要原因在于 ...
- Linq语法学习_增删篇。
关键词: select from where in into join on equals orderby descending thenby Table<TEntity> Default ...
- HTML_2
html图像 <img>标签可以在网页上插入一张图片,它是独立使用的标签,通过‘src’属性定义图片的地址(可为绝对路径也可为相对路径),通过‘alt’属性定义图片加载时显示的文字,以及对 ...
- 关于List的remove方法我遇到的坑
结果是下标越界异常,原因是remove方法的参数不是value,而是index 唉~~~ 年少轻狂啊
- 使用max函数计算EXCEL个税公式
1.Max()函数是求括号内的数的最大值.2.其中,第一和第二个大括号{}内的数,相信作为财务的应该很清楚,就是个人所得税的缴税比例,以及速算个人应缴所得税的相关数据.3.在EXCEL中,使用{}表示 ...
- 【原】基于matlab的蓝色车牌定位与识别---绪论
本着对车牌比较感兴趣,自己在课余时间摸索关于车牌的定位与识别,现将自己所做的一些内容整理下,也方便和大家交流. 考虑到车牌的定位涉及到许多外界的因素,因此有必要对车牌照的获取条件进行一些限定: 一.大 ...