【LeetCode】7、Reverse Integer(整数反转)
题目等级: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(整数反转)的更多相关文章
- 【LeetCode】Reverse Integer(整数反转)
这道题是LeetCode里的第7道题. 题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 ...
- [LeetCode]7. Reverse Integer整数反转
Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Examp ...
- LeetCode 7 Reverse Integer(反转数字)
题目来源:https://leetcode.com/problems/reverse-integer/ Reverse digits of an integer. Example1: x = 123, ...
- 【LeetCode】7. Reverse Integer 整数反转
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:整数,反转,题解,Leetcode, 力扣,Python, ...
- Leetcode7 : Reverse Integer 整数反转问题
问题描述 Example1: x = 123, return 321 Example2: x = -123, return -321 原题链接: https://leetcode.com/proble ...
- leetcode:Reverse Integer(一个整数反序输出)
Question:Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ...
- leetcode:Reverse Integer 及Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
- Leetcode#344. Reverse String(反转字符串)
题目描述 编写一个函数,其作用是将输入的字符串反转过来. 示例 1: 输入: "hello" 输出: "olleh" 示例 2: 输入: "A man ...
- LeetCode 7 Reverse Integer & int
Reverse Integer 想用余10直接算,没想到 -123%10 是 7, 原因 -123-(-123//10*10) r=a-n*[a/n] 以上,r是余数,a是被除数,n是除数. 唯一不同 ...
- Leetcode 7. Reverse Integer(水)
7. Reverse Integer Easy Given a 32-bit signed integer, reverse digits of an integer. Example 1: Inpu ...
随机推荐
- [办公应用]如何设置IE打印时的默认页边距,并设置纸张为横向(会计票据打印)
最近一个做会计的同事询问,如何将IE打印时的默认页边距更改,并且每次都要是横向的纸张. 这是因为她已经测试好纸张的大小,并据此调整好了页边距.可惜的是下一次打印时,又要重新调整一遍. 经过研究,方法如 ...
- 跨线程访问UI控件时的Lambda表达式
工作中经常会用到跨线程访问UI控件的情况,由于.net本身机制,是不允许在非UI线程访问UI控件的,实际上跨线程访问UI控件还是 将访问UI的操作交给UI线程来处理的, 利用Control.Invok ...
- Bootstrap 模态窗口源码分析
前言: bootstrap的 js插件的源码写的非常好,也算是编写jquery插件的模范写法,本来还想大篇详细的分析一下呢,唉,没时间啊,很早之前看过的源码了,现在贴在了博客上, 300来行的代码,其 ...
- 【Codevs 3115】高精度练习之减法
http://codevs.cn/problem/3115/ 板子题~ // <H.cpp> - Sun Oct 9 12:58:23 2016 // This file is made ...
- [Codeforces 482A] Diverse Permutation
[题目链接] https://codeforces.com/contest/482/problem/A [算法] 首先构造一个(k + 1)个数的序列 , 满足它们的差为1-k 对于i > k ...
- 4.7.4 Constructing LALR Parsing Tables
4.7.4 Constructing LALR Parsing Tables We now introduce our last parser construction method, the LAL ...
- JeePlus:目录
ylbtech-JeePlus:目录 1.返回顶部 0. http://www.jeeplus.org/ 0.2.文档 http://wiki.jeeplus.org/docs/show/75 0.3 ...
- 20. Extjs学习笔记——Ext.data.JsonStore使用说明
Ext.data.JsonStore继承于Ext.data.Store,使得从远程JSON数据创建stores更为方便的简单辅助类.JsonStore合成了Ext.data.HttpProxy与Ext ...
- mac+php+xdebug
1,下载xdebug 2,进入xdebug-2.4.0RC4目录,运行phpize命令, 2,google之后说要安装autoconf brew install autoconf 3,但是使用brew ...
- Spring通过注解注入有参
1.通过注解方式注入有参的构造函数 把@Autowired注解放在构造函数上方,在构造函数里写上需要注入的形参即可 2.通过XML配置文件方式定义有参构造函数