题目等级:Easy

题目描述:

Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123
Output: 321

Example 2:

Input: -123
Output: -321

Example 3:

Input: 120
Output: 21

题意:给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。


解题思路:

  本题很简单,我们给出以下两种方法。

  解法一:转为字符串

  由于之前我们大都做过如何反转字符串的题目,因此我们可以将整数转为字符串,通过字符串的反转来实现整数的反转,整数转字符串String.valueOf(num),字符串转整数Integer.parseInt(str)。

  这样的做法也很简单,但是实际上没有必要,因为直接完成整数的反转也比较简单。

class Solution {
public int reverse(int x) {
String s=String.valueOf(x);
if(x<0)
s=s.substring(1);
StringBuffer strBuf = new StringBuffer(s);
String str=strBuf.reverse().toString();
try{
return x>0 ? Integer.parseInt(str) : -1*Integer.parseInt(str);
}catch(NumberFormatException e){
return 0;
}
}
}

  解法二:弹出和压入数字

  通过pop = x % 10,取出末尾数字,然后除以 10 去掉个位数,然后用一个变量保存倒置的数。这个想法也很容易理解,但是这里的重点是如何防止溢出

  需要知道的是:int的范围是【-2147483648,2147483647】,也就是最大能表示的数大概是20亿左右,因此在反转的过程中,我们需要进行判断,是否溢出,如果溢出则返回0。

  具体过程参考如下代码:

class Solution {
public int reverse(int x) {
int res=0;
while(x!=0){
int temp=x%10;
x=x/10;
if (res > Integer.MAX_VALUE/10 || (res == Integer.MAX_VALUE / 10&&temp > 7)) return 0;
if (res < Integer.MIN_VALUE/10 || (res == Integer.MIN_VALUE / 10&&temp< -8)) return 0;
res=res*10+temp;
}
return res;
}
}

  实际上,这里的溢出判断略显复杂,可以用一种比较简单的方法:将反转后的数设置为long类型,然后在反转完成后,与int最大值和最小值进行比较,溢出则返回0。

public int reverse(int x) {
long res = 0;
while (x != 0) {
int temp = x % 10;
x /= 10;
res = res * 10 + temp;
}
if (res > Integer.MAX_VALUE || res < Integer.MIN_VALUE )
return 0;
return (int)res;
}

  时间复杂度:有多少位就循环多少次,所以是lg(n),即以10为底,时间复杂度O(lgn)

  空间复杂度:O(1)

总结:

  本题比较简单,思路不难想到,但是重点在于能否考虑到溢出,并正确的对溢出情况进行处理。

  从本题开始,由于感觉顺序刷题难度较大,转变一下刷题顺序,先刷easy难度的题。

【LeetCode】7、Reverse Integer(整数反转)的更多相关文章

  1. 【LeetCode】Reverse Integer(整数反转)

    这道题是LeetCode里的第7道题. 题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321  示例 2: 输入: -123 ...

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

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

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

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

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

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

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

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

  6. leetcode:Reverse Integer(一个整数反序输出)

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

  7. leetcode:Reverse Integer 及Palindrome Number

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

  8. Leetcode#344. Reverse String(反转字符串)

    题目描述 编写一个函数,其作用是将输入的字符串反转过来. 示例 1: 输入: "hello" 输出: "olleh" 示例 2: 输入: "A man ...

  9. LeetCode 7 Reverse Integer & int

    Reverse Integer 想用余10直接算,没想到 -123%10 是 7, 原因 -123-(-123//10*10) r=a-n*[a/n] 以上,r是余数,a是被除数,n是除数. 唯一不同 ...

  10. Leetcode 7. Reverse Integer(水)

    7. Reverse Integer Easy Given a 32-bit signed integer, reverse digits of an integer. Example 1: Inpu ...

随机推荐

  1. 怎样将DrawerLayout显示在ActionBar/Toolbar和status bar之间

    控制status bar utm_source=tuicool#toc_1" style="color:rgb(0,0,0); text-decoration:none; line ...

  2. 对“使用MyEclipse,写的jsp代码因有汉字而无法保存”问题的解决

    使用MyEclipse编辑jsp时.有时会出现"使用MyEclipse,写的jsp代码因有汉字而无法保存"的现象,怎样解决呢? Window-->Preferences--& ...

  3. 【bzoj4538】[Hnoi2016]网络

    我们考虑树剖,线段树上维护一个堆,保存不经过该段区间的路径的权值. 对于一条路径我们将对于线段树中的区间提取出来,在对于线段树中进行修改.也就是在堆中插入或删除. 对于一次询问,只要找到包含该点的线段 ...

  4. MongoDB全文搜索——目前尚不支持针对特定field的搜索

    > db.articles.createIndex( { subject: "text" } ) { "createdCollectionAutomatically ...

  5. mac 下idea光标问题

    可以使用 fn + i 解决此问题

  6. MySQL5.7修改字符集

    本人安装的mysql版本是5.7.20,安装好mysql后就要对字符集进行修改了,于是照着网上的大部分教程说的去安装目录找一个my-default.ini文件,然后重命名为my.ini,再对其进修改字 ...

  7. 01_c++下jni开发说明

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools= ...

  8. http-2.4

    http-2.4 1)新特性 (1)MPM 支持运行为DSO 机制:以模块形式按需加载 (2)event MPM 生产环境可用 (3)异步读写机制 (4)支持每模块及每目录的单独日志级别定义 (5)每 ...

  9. 【培训】MySQL

    yum安装mysql:yum -y install mysql*- 或者 yum -y install mysql* 启动数据库服务:/etc/init.d/mysqld start 或者 servi ...

  10. P1128 [HNOI2001]求正整数

    传送门 rqy是我们的红太阳没有它我们就会死 可以考虑dp,设\(dp[i][j]\)表示只包含前\(j\)个质数的数中,因子个数为\(i\)的数的最小值是多少,那么有转移方程 \[f[i][j]=m ...