【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 ...
随机推荐
- MySQL-数据表锁定
MySQL允许客户端会话明确获取表锁,以防止其他会话在特定时间段内访问表.客户端会话只能为自己获取或释放表锁.它不能获取或释放其他会话的表锁. 创建一个数据表: CREATE DATABASE IF ...
- tensorflow,torch tips
apply weightDecay,L2 REGULARIZATION_LOSSES weights = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIAB ...
- JavaScript基础 -- ECMAscript
ECMAScript是一种由Ecma国际(前身为欧洲计算机制造商协会,英文名称是European Computer Manufacturers Association)通过ECMA-262标准化的脚本 ...
- 【HNOI模拟By YMD】move
Description 设P(n)为从(0,0)移动到点(n,0)的不同路径数目,移动的方式有以下三种:(x,y)->(x+1,y-1),(x,y)->(x+1,y),(x+y)-> ...
- qemu常见选项解析
1 -hda file -hdb file.-hdc file.-hdd file 把文件当成hard disk 0.hard disk 1.hard disk 2和hard disk 3. 2 -a ...
- 浏览器对MP4视频 帧宽度 高度的兼容性
传入oss后 或者 本地 拖动到 浏览器 谷歌 都不能播放 Edge 可以播放 但 Edge不支持 本地拖入 播放 搜狗浏览器 同谷歌
- Yaf的性能
http://www.laruence.com/manual/yaf.bench.html 之前团队代码在ip的根下 http://192.168.6.212:83/js/common.js ; cd ...
- 【C++&爬虫】C++实现网络爬虫&socket初级教程
2019年了,发现以前的很多教程都不能用了. 我自己写的socket发给服务器总是返回301错误——资源永久转移.很多教程都是这样,困扰了我很久. 终于我发现了一篇能用的爬虫代码,参考MSDN以及众多 ...
- ExecuteNonQuery()的用法
ExecuteNonQuery()的用法 下面我们将详细讲解如何在Page_Load()中对数据库的增加.删除.修改,最后我们再来总结一下ExecuteNonQuery(),ExecuteScalar ...
- 【Dairy】2016.10.23 观火&中彩记
...................... 就第一条可以! 观火10分钟,长郡附近老房子起火...