File tree 1 file changed +1
-32
lines changed
src/main/java/com/fishercoder/solutions 1 file changed +1
-32
lines changed Original file line number Diff line number Diff line change 6
6
import java .util .Queue ;
7
7
import java .util .Set ;
8
8
9
- /**
10
- * 397. Integer Replacement
11
- *
12
- * Given a positive integer n and you can do operations as follow:
13
-
14
- If n is even, replace n with n/2.
15
- If n is odd, you can replace n with either n + 1 or n - 1.
16
- What is the minimum number of replacements needed for n to become 1?
17
-
18
- Example 1:
19
-
20
- Input:
21
- 8
22
-
23
- Output:
24
- 3
25
-
26
- Explanation:
27
- 8 -> 4 -> 2 -> 1
28
- Example 2:
29
-
30
- Input:
31
- 7
32
-
33
- Output:
34
- 4
35
-
36
- Explanation:
37
- 7 -> 8 -> 4 -> 2 -> 1
38
- or
39
- 7 -> 6 -> 3 -> 2 -> 1*/
40
9
public class _397 {
41
10
42
11
public static class Solution1 {
43
12
public int integerReplacement (int n ) {
44
13
long min = Long .MAX_VALUE ;
45
14
Set <long []> set = new HashSet ();
46
15
Queue <long []> q = new LinkedList ();
47
- long [] pair = new long [] {n , 0 };
16
+ long [] pair = new long []{n , 0 };
48
17
q .offer (pair );
49
18
while (!q .isEmpty ()) {
50
19
int size = q .size ();
You can’t perform that action at this time.
0 commit comments