题目等级: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. npm won't install packages “npm ERR! network tunneling socket could not be established, cause=Parse Error”

    昨天在使用npm安装react-native的时候一直报网络不能connection,可是在浏览器中直接访问时是成功,搜索百度无果,最后在google中找到了这个解决方案:http://stackov ...

  2. Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] B. Finding Team Member 排序

                                                                      B. Finding Team Member             ...

  3. c# Java 微信红包算法

    int total_money_cent = 1000; // 红包总金额 单位:分 int total_people = 8; // 抢红包总人数 int[] array = new int[tot ...

  4. scapy基础-网络数据包结构

    网络层次模型,数据包的组成是学习scapy的基础,下文主要关注模型中各个层次的用途,ethernet II和ip包数据结构.    1.五层模型简介 名称 作用 包含协议 应用层 面向程序对程序的传输 ...

  5. android developer官网不能打开怎么办

    映射网站: http://wear.techbrood.com

  6. [JSOI 2016] 最佳团体

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4753 [算法] 很明显的分数规划 可以用树形动态规划(树形背包)检验答案 时间复杂度 ...

  7. 8. Ext文本输入框:Ext.form.TextField属性汇总

    转自:https://blog.csdn.net/ryuudenne/article/details/8834650 Ext.form.TextField主要配置表: allowBlank       ...

  8. 2-4 原生小程序 - 自带组件及API

    获取用户的信息有两种,一种是不需要登录的,我们只需要获取用户的头像,还有微信的名.还有一种是登录的,就是登录后台校验的,就是获取用户的openid. webview,可以内嵌一个网页,类似于原生的开发 ...

  9. 慕课网2-5 编程练习:通过jQuery通配符选择器进行文字变色

    2-5 编程练习 请请使用*选择器将div标签中的字体颜色变成红色 效果图: 任务 (1)使用通配符选择器 (2)使用jQuery的.css()方法设置样式,语法css('属性 '属性值') 参考代码 ...

  10. ACM_Plants vs. Zombies(一元一次方程)

    Plants vs. Zombies Time Limit: 2000/1000ms (Java/Others) Problem Description: There is a zombie on y ...