题目等级: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. iOS MMDrawerController源码解读(一)

      提前说好,本文绝对不是教你如何使用MMDrawerController这个第三方库,因为那太多人写了 ,也太简单了.这篇文章主要带你分析MMDrawerController是怎么实现抽屉效果,明白 ...

  2. ubuntu双网卡绑定配置

    1,安装bonding需要的软件 sudo apt-get install ifenslave 2,在/etc/modules中加入: bonding mode= miimon= 3,在/etc/ne ...

  3. nohup 程序在后台运营 避免 xshell 卡死 通过 nohup.out分析调取系统命令时的异常分析

    nohup  程序在后台运营 避免 xshell  卡死 [root@admin1 after_fc_distributed]# nohup /root/anaconda3/bin/python da ...

  4. What does jQuery.fn mean?

    n jQuery, the fn property is just an alias to the prototype property. The jQuery identifier (or $) i ...

  5. Web 设计与开发者必须知道的 15 个站点

    新闻来源:catswhocode.com公司博客整整一个月没有更新了,最近一段时间,全公司都忙于两件事,为海尔集团做定制,为一个合作伙伴做 OEM,终于有了眉目.工作期间,常用到一些工具与帮助站点,今 ...

  6. 解决jQuery uploadify在非IE核心浏览器下无法上传

    之前上传了一个通过Flash实现多文件上传,但是在IE正常运行,FireFox 不能正常上传.经过反复研究学习,之所以firefox和360浏览器无法正常运行,是因为FireFox.chrome.36 ...

  7. Tool:Adobe Photoshop

    ylbtech-Tool-Adobe:Adobe Photoshop 1.返回顶部 1. Adobe Photoshop,简称“PS”,是由Adobe Systems开发和发行的图像处理软件. Pho ...

  8. 【171】IDL读取HDF文件

    ;+ ;:Description: ; Describe the procedure. ; ; Author: DYQ 2009-7-19; ; ;- PRO TEST_READHDF COMPILE ...

  9. debian下使用dpkg来安装/卸载deb包 (转载)

    转自:http://blog.csdn.net/zhou_2008/article/details/6076900 在debian下,你可以使用dpkg(Debian package system)来 ...

  10. sql server使用维护计划定时备份完整数据库、差异数据库

    我配置的是: 一个月执行一次完整备份数据库,删除三个月前备份文件.每天执行一次差异备份,删除一个月钱备份文件. 1.管理-维护计划   右键-新建维护计划 2.创建子计划 3.分别配置作业计划属性(执 ...