-
Notifications
You must be signed in to change notification settings - Fork 0
/
GrayCode.java
67 lines (60 loc) · 1.73 KB
/
GrayCode.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.util.ArrayList;
import java.util.Stack;
public class Solution {
public ArrayList<Integer> grayCodeFast(int n) {
ArrayList<Integer> results = new ArrayList<Integer>();
int bound = 1 << n;
for (int i = 0; i < bound; i++) {
results.add( (i >> 1) ^ i );
}
return results;
}
public ArrayList<Integer> grayCode2(int n) {
int[] code = new int[1];
ArrayList<Integer> results = new ArrayList<Integer>();
helper2(n, code, results);
return results;
}
private void helper2(int n, int[] code, ArrayList<Integer> results) {
if (n == 0) {
results.add(code);
return;
}
helper2(n - 1, code, results);
int mask = 1 << (n - 1);
code[0] ^= mask;
helper2(n - 1, code, results);
}
// treat the inverse positions as inorder traversal
// elements of a binary tree of the form:
// n
// / \
// n-1 n-1
// / \ / \
// n-2 n-2 n-2 n-2
// ......
public ArrayList<Integer> grayCode(int n) {
int code = 0;
ArrayList<Integer> results = new ArrayList<Integer>();
results.add(code);
Stack<Integer> positions = new Stack<Integer>();
while (!positions.isEmpty() || n > 0) {
if (n > 0) {
positions.push(n);
n--;
}
else {
n = positions.pop();
code = inverse(code, n);
results.add(code);
n--;
}
}
return results;
}
private int inverse(int code, int pos) {
int mask = 1 << (pos - 1);
code ^= mask;
return code;
}
}