ARTS leetcode2 reverse integer
- 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
Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−2^31, 2^31 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
题目意思解释:翻转数字
给你一个32位有符号的integer数字,对该值进行反转得到一个新的数值。
需要注意的是假设只能处理-231到231-1之间的数值,如果你的翻转结果超过了范围,那么就让函数返回0.
我的思路:
1.数值划分为正数和负数以及只有一位的数值,如果只有1位,那么就直接返回,否则就按照正数和负数来处理。
2.因为有符号,所以负数的翻转也就相当于正数的翻转,最后加上符号位,到这里可以看出正负数可以统一处理,前提是将负值取绝对值。
3.翻转核心:数字转为字符串,然后转字符数组,然后头尾调换,最后再将字符数组转字符串再转int型,如果超出范围就进行异常处理,返回0,否则正常返回。
我遇到的坑就是:忘了进行异常处理,直接用了Integer.parseInt(String.valueOf(ch));
结果就是抛出了异常NumberFormatException
,我还想为啥别人的没有抛出这个错误,,,,,最后重新读题才发现,最后一句话给了提示,超出范围直接返回0.
下面是我的主要代码实现:
class Solution {
public int reverse(int x) {
if(x<Integer.MIN_VALUE || x>Integer.MAX_VALUE){
throw new IllegalArgumentException("x is too large or too small");
}
if(x > -10 && x < 10){
return x;
}
if(x < 0){
return -reverseInteger(-x);
}else{
return reverseInteger(x);
}
}
private static int reverseInteger(int num) {
char[] ch = Integer.toString(num).toCharArray();
int right = ch.length-1;
for ( int left = 0; left < right ; left++ ,right--)
{
char temp = ch[left];
ch[left] = ch[right];
ch[right]=temp;
}
try{
return Integer.parseInt(String.valueOf(ch));
}catch (NumberFormatException e){
return 0;
}
}
}
运行结果:
Runtime: 2 ms, faster than 100.00% of Java online submissions for Reverse Integer.
Memory Usage: 32.7 MB, less than 100.00% of Java online submissions for Reverse Integer.
看了一下discuss 中关于java的算法实现:
这位大佬Blankj的实现很简单,也容易看懂:
class Solution {
public int reverse(int x) {
long res = 0;
for (; x != 0; x /= 10)
res = res * 10 + x % 10;
return res > Integer.MAX_VALUE || res < Integer.MIN_VALUE ? 0: (int) res;
}
}
他采用long型来保存结果,
核心:
x!=0//进行下面两步循环,把低位数值取出来放在高位。最后检查边界值
res = res*10 + res/10;
x/=10;
Runtime: 1 ms, faster than 100.00% of Java online submissions for Reverse Integer.
Memory Usage: 32.5 MB, less than 100.00% of Java online submissions for Reverse Integer.
真的简单易懂,自己第一次就没有想到这种方法。。。。
下面再说一种使用字符串来解决的(作者:seanoh1989):
class Solution {
public int reverse(int x) {
StringBuilder result = new StringBuilder();
String resultSt = "";
int val = 0;
if (x > 0) {
resultSt = result.append(x).reverse().toString();
try {
val = (int) Integer.parseInt(resultSt);
} catch (NumberFormatException e) { return 0; }
return val;
}
else {
x = -x;
resultSt = result.append(x).reverse().toString();
try {
val = (int) Integer.parseInt(resultSt);
} catch (NumberFormatException e) { return 0; }
return -val;
}
}
}
Runtime: 2 ms, faster than 100.00% of Java online submissions for Reverse Integer.
Memory Usage: 32.6 MB, less than 100.00% of Java online submissions for Reverse Integer.
他也分正负数,然后他采用了reverse反转方法,直接调用,然后转int型捕获异常。
再看一个大佬写的,用了Java8来搞的,代码如下:
class Solution {
public int reverse(int x) {
int r = 0;
while (x!=0) {
try{
//这一步就相当于r = r*10;
r = Math.multiplyExact(r,10);
//这一步相当于r = r+x%10;
r = Math.addExact(r, x%10);
}
catch (ArithmeticException e){
return 0;
}
x = x/10;
}
return r;
}
}
关于这两个方法的源码,我会在另外一篇文章中写出来。
Math上面两个方法源码链接
目前看到对于这道题的解答思路两个:
(1)使用字符串逆序
(2)使用除法和求余加循环