Skip to content

Commit 02934b3

Browse files
refactor 397
1 parent 0270892 commit 02934b3

File tree

2 files changed

+68
-42
lines changed

2 files changed

+68
-42
lines changed

src/main/java/com/fishercoder/solutions/_397.java

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
import java.util.Queue;
77
import java.util.Set;
88

9-
/**Given a positive integer n and you can do operations as follow:
9+
/**
10+
* 397. Integer Replacement
11+
*
12+
* Given a positive integer n and you can do operations as follow:
1013
1114
If n is even, replace n with n/2.
1215
If n is odd, you can replace n with either n + 1 or n - 1.
@@ -36,54 +39,46 @@
3639
7 -> 6 -> 3 -> 2 -> 1*/
3740
public class _397 {
3841

39-
public static int integerReplacement(int n) {
40-
long min = Long.MAX_VALUE;
41-
Set<long[]> set = new HashSet();
42-
Queue<long[]> q = new LinkedList();
43-
long[] pair = new long[]{n, 0};
44-
q.offer(pair);
45-
while (!q.isEmpty()) {
46-
int size = q.size();
47-
for (int i = 0; i < size; i++) {
48-
long[] curr = q.poll();
49-
if (curr[0] == 1) {
50-
set.add(curr);
51-
} else {
52-
53-
if (curr[0] % 2 == 0) {
54-
curr[0] /= 2;
55-
curr[1]++;
56-
q.offer(curr);
42+
public static class Solution1 {
43+
public int integerReplacement(int n) {
44+
long min = Long.MAX_VALUE;
45+
Set<long[]> set = new HashSet();
46+
Queue<long[]> q = new LinkedList();
47+
long[] pair = new long[] {n, 0};
48+
q.offer(pair);
49+
while (!q.isEmpty()) {
50+
int size = q.size();
51+
for (int i = 0; i < size; i++) {
52+
long[] curr = q.poll();
53+
if (curr[0] == 1) {
54+
set.add(curr);
5755
} else {
58-
long[] minus = new long[2];
59-
minus[0] = curr[0] - 1;
60-
minus[1] = curr[1] + 1;
61-
q.offer(minus);
6256

63-
long[] plus = new long[2];
64-
plus[0] = curr[0] + 1;
65-
plus[1] = curr[1] + 1;
66-
q.offer(plus);
57+
if (curr[0] % 2 == 0) {
58+
curr[0] /= 2;
59+
curr[1]++;
60+
q.offer(curr);
61+
} else {
62+
long[] minus = new long[2];
63+
minus[0] = curr[0] - 1;
64+
minus[1] = curr[1] + 1;
65+
q.offer(minus);
66+
67+
long[] plus = new long[2];
68+
plus[0] = curr[0] + 1;
69+
plus[1] = curr[1] + 1;
70+
q.offer(plus);
71+
}
6772
}
6873
}
6974
}
70-
}
7175

72-
Iterator<long[]> it = set.iterator();
73-
while (it.hasNext()) {
74-
min = Math.min(min, it.next()[1]);
76+
Iterator<long[]> it = set.iterator();
77+
while (it.hasNext()) {
78+
min = Math.min(min, it.next()[1]);
79+
}
80+
return (int) min;
7581
}
76-
return (int) min;
7782
}
7883

79-
public static void main(String... strings) {
80-
System.out.println(integerReplacement(2147483647));
81-
System.out.println(integerReplacement(65535));//should be 17
82-
System.out.println(integerReplacement(1234));//should be 14
83-
// System.out.println(integerReplacement(1));
84-
// System.out.println(integerReplacement(2));
85-
// System.out.println(integerReplacement(3));
86-
System.out.println(integerReplacement(5));//should be 3
87-
// System.out.println(integerReplacement(7));
88-
}
8984
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._397;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _397Test {
10+
private static _397.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _397.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(17, solution1.integerReplacement(65535));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(14, solution1.integerReplacement(1234));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(3, solution1.integerReplacement(5));
30+
}
31+
}

0 commit comments

Comments
 (0)