【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 ...
随机推荐
- ubuntu下irobot串口通讯
在window下以前就`有一个现成的串口代码.想移植到ubuntu下,发现都不一样了.要重新找个. 折腾了一上午之后,发现自己写这个串口通讯还是有一点难度. 于是,用了github上 Erick Co ...
- GetRelativePath获取相对路径
public static string GetRelativePath(string baseDirPath, string subFullPath) { // ForceBasePath to a ...
- 【虚拟机-网络IP】如何开放 Azure 虚拟机 Ping 功能
前言 文章<使用 PsPing & PaPing 进行 TCP 端口连通性测试>中提到,ICMP 协议的数据包无法通过 Azure 的防火墙和负载均衡器,所以不能直接使用 Ping ...
- (转载)WPF:DataGrid设置行、单元格的前景色
WPF:DataGrid设置行.单元格的前景色 0. 说明 /********************************** 本示例实现功能1.DataGrid基本操作2.列标题样式3.内容居中 ...
- jsp另外五大内置对象之out输出
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding= ...
- Spark Job调优(Part 2)
原文链接:https://wongxingjun.github.io/2016/05/11/Spark-Job%E8%B0%83%E4%BC%98-Part-2/ 这篇文章将会完成Part 1中留下的 ...
- Paper: TranE
论文标题:Translating Embeddings for Modeling Multi-relational Data 标题翻译:多元关系数据翻译嵌入建模 摘要: 考虑多元关系数据的实体和关系在 ...
- CPP-基础:新标准 C++iostream
在新的标准 C++ iostream 库中: 1. open 函数不采用第三个参数(保护参数). 2. 无法从文件句柄创建流. 3. 除了几个例外,新的标准 C++ 库中的所有名称都在 std 命名空 ...
- wine卸载
Wine手动卸载,出现殘留,导致安装其他软件也不成功. 错误如下: 正在读取软件包列表... 完成正在分析软件包的依赖关系树 正在读取状态信息... 完成 您也许需要运行“ap ...
- Objective-C实现一个简单的栈
栈作为一种数据结构,是一种只能在一端进行插入和删除操作的特殊线性表.它按照先进后出的原则存储数据,先进入的数据被压入栈底,最后的数据在栈顶,需要读数据的时候从栈顶开始弹出数据(最后一个数据被第一个读出 ...