-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReverseInteger.java
58 lines (47 loc) · 1.23 KB
/
ReverseInteger.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
43
44
45
46
47
48
49
50
package brian;
/**
* Created by brian on 7/23/17.
*/
public class ReverseInteger {
public static int reverseByString(int x) {
boolean negative = false;
if (x < 0) {
negative = true;
x *= -1;
}
String inputString = Integer.toString(x);
String s = new StringBuilder(inputString).reverse().toString();
int answer;
try {
answer = Integer.parseInt(s);
} catch (NumberFormatException e) {
return 0;
}
if (negative) {
answer *= -1;
}
return answer;
}
public static int reverseByModulo(int x) {
int answer = 0;
while (x != 0) {
// -2147483648~2147483647
if (answer > 214748364 || answer < -214748364 ||
(answer == 214748364 && x % 10 > 7) ||
(answer == -214748364 && x % 10 < -8)
) {
return 0;
}
// 123
answer = answer * 10 + x % 10;
// 0 + 3 => 3
// 30 + 2 => 32
// 320 + 1 => 321
x /= 10;
// 12
// 1
// 0
}
return answer;
}
}