LeetCode刷题之Reverse_Integer_0007

java版本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public static void main(String[] args) {

System.out.println(reverse_plus(-123));

}


// 数字转字符数组 ===>倒叙,再转数字
public static int reverse(int x) {

String strX = "";
boolean flag = false;
if(x < 0){
x = -x;
flag = true;
}

strX += x;
char[] ArrX = strX.toCharArray();
strX = "";
for (int i = ArrX.length-1; i >=0; i--){
strX += ArrX[i];
}

try{
return flag? -Integer.parseInt(strX):Integer.parseInt(strX);
}catch (NumberFormatException e){
return 0;
}
}

//纯数字游戏 倒着数数
public static int reverse_plus(int x) {

long n = 0;
while (x!=0){
n = n * 10 + x%10;
x = x / 10;
}
return (int)n == n? (int) n : 0;

}
苏小南 wechat
扫一扫,点个关注哦!
生活不易,记得打个赏