这道题是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. Redis数据库2

    一.存储list 1.概述     list类型是按照插入顺序排序的字符串链表,可以在头部(left)和尾部(right)添加新的元素,如果不存在,则自动创建;如果list中所有元素被移除,则list ...

  2. python中__file__

    用__file__ 来获得脚本所在的路径,比如文件在/root下 cat tee #!/usr/bin/env pythonprint __file__              #得到相对路径tee ...

  3. 备份和导入Outlook 2016 电子邮件签名

    在本文中,我将分享您在Outlook 2013和Outlook 2016中备份或导入签名的过程 在清除Outlook配置文件之前,请确保您通过在文件资源管理器中的配置文件中的APPDATA文件夹中复制 ...

  4. NVM for Windows下载与安装

    下载NVM for Windows https://github.com/coreybutler/nvm-windows/releases nvm-noinstall.zip: 这个是绿色免安装版本, ...

  5. COGS 2104. [NOIP2015]神奇的幻方

    ★   输入文件:2015magic.in   输出文件:2015magic.out   简单对比时间限制:1 s   内存限制:256 MB 模拟 一开始数组开小了.. 屠龙宝刀点击就送 #incl ...

  6. vue props 传入对象Object,如果外层更改属性,默认里面是不更新,需要使用 this.$set(this.datese1, 'xsfaDateYear1', '')

    vue props 传入对象Object,如果外层更改属性,默认里面是不更新,需要使用 this.$set(this.datese1, 'xsfaDateYear1', '')

  7. 使用控件的Tag属性传递信息

    实现效果: 知识运用: Control类的Tag属性 //获取或设置包含 有关控件的数据的对象 public object Tag {get;set;} 实现代码: private void Form ...

  8. 第三单元OO总结

  9. 修改Windows默认调试器

    程序崩溃时,系统会弹窗让你选择是否进行调试,可以设置系统默认调试器. 注册表位置: HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows NT/CurrentVe ...

  10. javaweb基础(21)_两种开发模式

    SUN公司推出JSP技术后,同时也推荐了两种web应用程序的开发模式,一种是JSP+JavaBean模式,一种是Servlet+JSP+JavaBean模式. 一.JSP+JavaBean开发模式 1 ...