-
Notifications
You must be signed in to change notification settings - Fork 1
/
LeetCode-7-Reverse-Integer.java
104 lines (79 loc) · 2.69 KB
/
LeetCode-7-Reverse-Integer.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
LeetCode: https://leetcode.com/problems/reverse-integer/
LintCode: http://www.lintcode.com/problem/reverse-integer/
JiuZhang: http://www.jiuzhang.com/solutions/reverse-integer/
ProgramCreek: http://www.programcreek.com/2012/12/leetcode-reverse-integer/
Other: http://www.cnblogs.com/yuzhangcmu/p/4162196.html
Analysis:
This question focus on reverse may out of Integer range. So we need to set reverse as double, and judge if it is > Integer.MAX_VALUE or < Integer.MIN_VALUE, if so return 0.
Test cases:
"1534236469" --> "0"
*/
public class Solution {
// not good solution
// public int reverse(int x) {
// boolean isPositive = true;
// if(x < 0) {
// isPositive = false;
// x = -x;
// }
// double reverse = 0;
// int p = x;
// while(p > 0){
// reverse = p % 10 + 10 * reverse;
// p /= 10;
// }
// if(reverse > Integer.MAX_VALUE) return 0;
// if(!isPositive){
// reverse = -reverse;
// }
// return (int)reverse;
// }
// 2.
// public int reverse(int x) {
// boolean isPositive = true;
// if (x < 0) {
// isPositive = false;
// x = -1 * x;
// }
// char[] chars = (x + "").toCharArray();
// int lo = 0, hi = chars.length - 1;
// while (lo < hi) {
// swap(chars, lo, hi);
// lo++;
// hi--;
// }
// long res = 0L;
// for (int i = 0; i < chars.length; i++) {
// res = 10 * res + chars[i] - '0';
// }
// if (res > Integer.MAX_VALUE || res < Integer.MIN_VALUE) return 0;
// if (!isPositive) return -1 * (int) res;
// return (int) res;
// }
// private void swap(char[] chars, int lo, int hi) {
// char temp = chars[lo];
// chars[lo] = chars[hi];
// chars[hi] = temp;
// }
// Wrong solution, as it's possible for reverse out of integer size
// public int reverse(int x) {
// int reverse = 0;
// while(x != 0){
// reverse = 10 * reverse + x % 10;
// x /= 10;
// }
// return reverse;
// }
// Correct solution, succinct solution
public int reverse(int x) {
double reverse = 0;
while(x != 0){
reverse = 10 * reverse + x % 10;
x /= 10;
}
// If reverse out of Integer range, return 0
if(reverse > Integer.MAX_VALUE || reverse < Integer.MIN_VALUE) return 0;
return (int)reverse;
}
}