Skip to content

Commit a9f2754

Browse files
authored
Create 089. Gray Code.java
1 parent 7e559b3 commit a9f2754

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

089. Gray Code.java

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// https://leetcode.com/problems/gray-code
2+
// magical bit manipulation see https://en.wikipedia.org/wiki/Gray_code
3+
// doubt that it's possible to come up with this solution during 30 min interview without gray code familiarity!!!
4+
// though it might be possible to look at the reflection pattern and do it the long way
5+
public class Solution {
6+
public List<Integer> grayCode(int n) {
7+
List<Integer> result = new LinkedList<>();
8+
9+
for (int i = 0; i < 1<<n; i++)
10+
result.add(i ^ i>>1);
11+
12+
return result;
13+
}
14+
}

0 commit comments

Comments
 (0)