Skip to content

Commit 2e63b32

Browse files
[N-0] refactor 7
1 parent 4c11c2d commit 2e63b32

File tree

2 files changed

+42
-73
lines changed

2 files changed

+42
-73
lines changed
Lines changed: 19 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,28 @@
11
package com.fishercoder.solutions;
2-
/**7. Reverse Integer
3-
Reverse digits of an integer.
42

5-
Example1: x = 123, return 321
6-
Example2: x = -123, return -321*/
7-
public class _7 {
8-
public int reverse_short_version(int x) {
9-
long rev = 0;
10-
while (x != 0) {
11-
rev = rev * 10 + x % 10;
12-
x /= 10;
13-
if (rev > Integer.MAX_VALUE || rev < Integer.MIN_VALUE) {
14-
return 0;
15-
}
16-
}
17-
return (int) rev;
18-
}
3+
/**
4+
* 7. Reverse Integer
5+
*
6+
* Reverse digits of an integer.
197
20-
public int reverse(int x) {
21-
if (x == 0) {
22-
return 0;
23-
}
24-
//save the first bit if it's a negative sign
25-
StringBuilder sb = new StringBuilder();
26-
sb.append(x);
27-
boolean negative = sb.toString().charAt(0) == '-' ? true : false;
28-
//use modulor and division and use long as the result type to avoid overflow
29-
long longX = (long) x;
30-
if (negative) {
31-
//get rid of the first '-' bit
32-
String withoutNegativeSign = sb.substring(1).toString();
33-
longX = Long.parseLong(withoutNegativeSign);
34-
}
35-
sb.setLength(0);
36-
long result = 0;
37-
if (negative) {
38-
sb.append('-');
39-
}
40-
while (longX != 0) {
41-
sb.append(longX % 10);
42-
longX /= 10;
43-
}
44-
result = Long.parseLong(sb.toString());
45-
System.out.println(result);//it's right here, but after converting it into an int, it overflowed to become a wrong number, how to handle this?
46-
//it turns out depending on the question requirement, on this OJ, it's expecting 0, if it's beyond the range of (Integer.MIN_VALUE, Integer.MAX_VALUE)
47-
return (result > Integer.MAX_VALUE || result < Integer.MIN_VALUE) ? 0 : (int) result;
48-
}
8+
Example1: x = 123, return 321
499
50-
public static void main(String... strings) {
51-
_7 test = new _7();
52-
//when the input is 1534236469, it's expecting 0 as the correct answer, this is due to its reversed number is greater than Integer.MAX_VALUE, thus return 0
53-
System.out.println(1534236469 > Integer.MAX_VALUE);
54-
System.out.println("1534236469\n" + Integer.MAX_VALUE);
55-
// System.out.println(test.reverse(-2147483648));
56-
}
10+
Example2: x = -123, return -321
5711
58-
// this is not going to work when the input number's reverse version is greater than
59-
// Integer.MAX_VALUE, it'll throw NumberFormatException as Java cannot handle it, overflowed.
60-
public int reverse_overflowed(int x) {
61-
//save the first bit if it's a negative sign
62-
StringBuilder sb = new StringBuilder();
63-
sb.append(x);
64-
boolean negative = sb.toString().charAt(0) == '-' ? true : false;
65-
char[] bits = sb.toString().toCharArray();
66-
sb.setLength(0);
67-
if (negative) {
68-
sb.append('-');
69-
//until i > 0
70-
for (int i = bits.length - 1; i > 0; i--) {
71-
sb.append(bits[i]);
72-
}
73-
} else {
74-
//until i >= 0
75-
for (int i = bits.length - 1; i >= 0; i--) {
76-
sb.append(bits[i]);
12+
*/
13+
public class _7 {
14+
15+
public static class Solution1 {
16+
public int reverse(int x) {
17+
long rev = 0;
18+
while (x != 0) {
19+
rev = rev * 10 + x % 10;
20+
x /= 10;
21+
if (rev > Integer.MAX_VALUE || rev < Integer.MIN_VALUE) {
22+
return 0;
23+
}
7724
}
25+
return (int) rev;
7826
}
79-
return Integer.parseInt(sb.toString());
8027
}
81-
8228
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._7;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _7Test {
10+
private static _7.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _7.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
// its reversed number is greater than Integer.MAX_VALUE, thus return 0
20+
assertEquals(0, solution1.reverse(1534236469));
21+
}
22+
23+
}

0 commit comments

Comments
 (0)