7. Reverse Integer
Difficulty: Easy
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
1 | **Input:** 123 |
Example 2:
1 | **Input:** -123 |
Example 3:
1 | **Input:** 120 |
Note:
Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
My Solution
最简单解法,直接用系统的parseInt1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18class Solution {
public int reverse(int x) {
String ts = Math.abs(x)+"";
StringBuilder sb=new StringBuilder();
for(int i=ts.length()-1;i>=0;--i){
sb.append(ts.charAt(i));
}
try{
if(x<0){
return Integer.parseInt("-"+sb.toString());
}else{
return Integer.parseInt(sb.toString());
}
}catch(Exception e){
return 0;
}
}
}
取余解法1
2
3
4
5
6
7
8
9
10
11
12class Solution {
public int reverse(int x) {
long rev= 0;
while( x != 0){
rev= rev*10 + x % 10;
x= x/10;
if( rev > Integer.MAX_VALUE || rev < Integer.MIN_VALUE)
return 0;
}
return (int) rev;
}
}