Skip to content

Commit cf607df

Browse files
author
Tushar Roy
committed
Get first n gray code
1 parent 739af2a commit cf607df

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.interview.bits;
2+
3+
import sun.java2d.xr.GrowableRectArray;
4+
5+
import java.util.LinkedList;
6+
import java.util.List;
7+
8+
/**
9+
* Date 03/12/2016
10+
* @author Tushar Roy
11+
*
12+
* Get first n gray code
13+
* Reference
14+
* https://leetcode.com/problems/gray-code/
15+
*/
16+
public class GrayCode {
17+
public List<Integer> grayCode(int n) {
18+
List<Integer> result = new LinkedList<>();
19+
for (int i = 0; i < 1<<n; i++) {
20+
result.add(i ^ i>>1);
21+
}
22+
return result;
23+
}
24+
25+
public static void main(String args[]) {
26+
GrayCode gc = new GrayCode();
27+
System.out.println(gc.grayCode(4));
28+
}
29+
}

0 commit comments

Comments
 (0)