-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathSolution.java
32 lines (26 loc) · 896 Bytes
/
Solution.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
import java.util.Arrays;
/**
* @author Oleg Cherednik
* @since 16.02.2019
*/
public class Solution {
public static void main(String... args) {
int length = 3;
Arrays.stream(grayCode(length))
.mapToObj(code -> String.format("%" + length + 's', Integer.toBinaryString(code)).replace(' ', '0'))
.forEach(System.out::println);
}
public static int[] grayCode(int n) {
return gray(0, n, new int[(int)Math.pow(2, n)]);
}
private static int[] gray(int curLength, int maxLength, int[] codes) {
if (curLength > maxLength)
return codes;
if (curLength == 0)
codes[0] = 0;
else
for (int i = 0, t = 1 << (curLength - 1); i < t; i++)
codes[t + i] = codes[t - i - 1] + (1 << (curLength - 1));
return gray(curLength + 1, maxLength, codes);
}
}