这道题是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(整数反转)的更多相关文章

  1. 【LeetCode】7. Reverse Integer 整数反转

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:整数,反转,题解,Leetcode, 力扣,Python, ...

  2. [LeetCode]7. Reverse Integer整数反转

    Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...

  3. Leetcode7 : Reverse Integer 整数反转问题

    问题描述 Example1: x = 123, return 321 Example2: x = -123, return -321 原题链接: https://leetcode.com/proble ...

  4. [Leetcode] reverse integer 反转整数

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to ...

  5. LeetCode 7 Reverse Integer(反转数字)

    题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...

  6. Leetcode(力扣) 整数反转

    Leetcode 7.整数反转 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例: 输入: -123 输出: -321 注意: 假设我们的环境只能存储得下 32 位的有符 ...

  7. LeetCode Golang 7. 整数反转

    7. 整数反转 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. Tips : Math包给出的类型大小的边界: // Integer limit values. const ...

  8. 每日一道 LeetCode (2):整数反转

    题目:整数反转 题目来源:https://leetcode-cn.com/problems/reverse-integer 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示 ...

  9. LeetCode: Reverse Integer 解题报告

    Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...

随机推荐

  1. I Have a Dream(我有一个梦想)

    I Have a Dream by Martin Luther King, Jr. I am happy to join with you today in what will go down in ...

  2. SQL Server Sleeping会话占用内存资源浅析?

      在SQL Server中,会话的状态有运行(Running).睡眠(Sleeping).休眠(Dormant).Preconnect 等状态,有时候你会在数据库中看到很多会话处于睡眠(Sleepi ...

  3. .NET 读取视频文件

    该篇文章 复制别人的文章 在.NET中处理视频是一件痛苦的事情,.NET并没有提供视频处理的类.于是咱们只能找一些第三方的类库或者自己实现,在项目时间比较赶的情况下,自己实现是不可能的了,而且说不定会 ...

  4. Ubuntu下手动安装NextCloud

    安装环境:阿里云VPS Ubuntu 16.04   一. 安装Apache2 sudo apt-get install apache2 安装完成后,浏览器访问http://your ip/,出现It ...

  5. 2018.5.6 解决问题:oracle------ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务

    解决问题:ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务 或者是重启电脑之后无法进入控制台企业管理器(OEM)图形化界面(重新添加注入监听器就行了 文件listener.org) ...

  6. 面向对象OONo.3单元总结

    一,JML语言 1)JML理论基础:JML是一类语言,用来描述一个方法或一个类的功能.以及这个类在实现这个功能时需要的条件.可能改变的全局变量.以及由于条件问题不能实现功能时这个方法或类的行为,具有明 ...

  7. <!DOCTYPE>声明

    定义和用法 <!DOCTYPE> 声明必须是 HTML 文档的第一行,位于 <html> 标签之前. <!DOCTYPE> 声明不是 HTML 标签:它是指示 we ...

  8. cron job 里面,如何让脚本半分钟运行一次?

    cron job 里面,如何让脚本半分钟运行一次? cron本身实现不了.但可以借助于sleep命令实现. 解决方法: 两个脚本 一个正常,一个增加sleep 30 crontab设置同时启动 在有的 ...

  9. scipy学习之(二)——io操作及其misc操作对图片的处理

    SciPy有许多模块.类和函数,io子模块可用于从各种文件格式中读取数据和将数据写入各种文件格式. from scipy import io import numpy as np 生成数据 data ...

  10. Linux下同进程多进程号实时监控

    一.需求: Linux上对一个进程名称可能会对应的多个进程号的进程进行监控,如果有多个则输出到一个日志文件. 以上问题针对的是一个定时程序还未运行结束,到下一个时刻程序又运行起来了,避免造成重复调用接 ...