Skip to content

Commit b7905b8

Browse files
committed
grayCode
1 parent 211d45b commit b7905b8

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/grayCode/grayCode.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public class Solution {
2+
public List<Integer> grayCode(int n) {
3+
List<Integer> result = new ArrayList<Integer>();
4+
if (n == 0) {
5+
result.add(0);
6+
return result;
7+
}
8+
int d = 1<<(n-1);
9+
result = grayCode(n-1);
10+
int size = result.size();
11+
for (int i=size-1; i>=0; i--) {
12+
result.add(result.get(i) + d);
13+
}
14+
15+
return result;
16+
}
17+
}

0 commit comments

Comments
 (0)