Skip to content

Commit 30bdd4c

Browse files
refactor 89
1 parent 2d01f2f commit 30bdd4c

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

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

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.fishercoder.solutions;
22

3-
import com.fishercoder.common.utils.CommonUtils;
4-
53
import java.util.ArrayList;
64
import java.util.List;
75

@@ -31,19 +29,14 @@
3129

3230
public class _89 {
3331

32+
public static class Solution1 {
3433
public List<Integer> grayCode(int n) {
35-
List<Integer> result = new ArrayList();
36-
for (int i = 0; i < (1 << n); i++) {
37-
result.add(i ^ (i >> 1));
38-
}
39-
return result;
34+
List<Integer> result = new ArrayList();
35+
for (int i = 0; i < (1 << n); i++) {
36+
result.add(i ^ (i >> 1));
37+
}
38+
return result;
4039
}
40+
}
4141

42-
public static void main(String... args) {
43-
int n = 3;
44-
System.out.println("1 << n = " + (1 << n));
45-
_89 test = new _89();
46-
List<Integer> result = test.grayCode(n);
47-
CommonUtils.printList(result);
48-
}
4942
}
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._89;
4+
import java.util.Arrays;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class _89Test {
11+
12+
private static _89.Solution1 solution1;
13+
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _89.Solution1();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
assertEquals(Arrays.asList(0, 1, 3, 2, 6, 7, 5, 4), solution1.grayCode(3));
22+
}
23+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy