【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 ...
随机推荐
- 访问权限修饰符-static-final-this-super-匿名对象
1.this关键字的作用 1)调用本类中的属性; 2)调用本类中的构造方法;且只能放首行,且必须留一个构造方法作为出口,即不能递归调用 3)表示当前对象; 2.匿名对象 ...
- ios 12 xcode10 新升级的编译报错libstdc++.6.0.9 Multiple commands produce
问题一 编译报错 Showing Recent Messages :-1: Multiple commands produce '/Users/duning/Library/Developer/Xco ...
- wpf ListBox删除选择项(支持多项)
搞了个ListBox删除选择项,开始老是不能把选择项删除干净,剩下几个.后来调试一下原来是ListBox在删除一个选择项之后立即更新,选择项也有变化.结果我想了个这样的方法来删除呵呵. Departm ...
- HDU 1124 Factorial (阶乘后缀0)
题意: 给一个数n,返回其阶乘结果后缀有几个0. 思路: 首先将n个十进制数进行质因数分解,观察的得到只有2*5才会出现10.那么n!应含有min(2个数,5个数)个后缀0,明显5的个数必定比2少,所 ...
- 使用ServiceController组件控制计算机服务
实现效果: 知识运用: ServiceController组件的MachineName属性 //获取或设置服务所驻留的计算机名称 public string MachineName{get;set;} ...
- JS事件类型--1
滚轮事件其实就是一个mousewheel事件,这个事件跟踪鼠标滚轮,类似Mac的触屏版. 一.客户区坐标位置 鼠标事件都是在浏览器视口的特定位置上发生的.这个位置信息保存在事件对象的clientX和c ...
- Dojo的on函数(以前的dojo.connect)
同jQuery的on函数: require(["esri/map", "dojo/on"], function(Map, on) { // ... on(my ...
- 【点分治】luoguP2664 树上游戏
应该是一道中等难度的点分?麻烦在一些细节. 题目描述 lrb有一棵树,树的每个节点有个颜色.给一个长度为n的颜色序列,定义s(i,j) 为i 到j 的颜色数量.以及 现在他想让你求出所有的sum[i] ...
- CentOS7系统引导顺序以及排障
引导顺序 UEFi或BIOS初始化,运行POST开机自检 选择启动设备 引导装载程序, centos7是grub2 加载装载程序的配置文件:/etc/grub.d/ /etc/default/gru ...
- destoon模块自定义字段的添加并让其支持搜索的方法
今天看了看模块设置里的自定义字段功能的用法,试着加了个新字段glry,设置了值,然后去数据库moduleid的article表看,字段成功加上了. 于是去template下article文件夹的lis ...