- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrayCode_89.java
More file actions
Latest commit
28 lines (26 loc) · 634 Bytes
/
Copy pathGrayCode_89.java
File metadata and controls
28 lines (26 loc) · 634 Bytes
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
/**
* 0 n = 1
* 1
* ------------------------
* 1 1 n = 2
* 1 0
* ------------------------
* 1 1 0 n = 3
* 1 1 1
* 1 0 1
* 1 0 0
* ...
*/
publicclassSolution {
publicList<Integer> grayCode(intn) {
List<Integer> list = newArrayList<>(Arrays.asList(0));
if (n == 0) returnlist;
list.add(1);
for(inti=2; i<=n; i++) {
intlength = list.size();
for(intj=length-1; j>=0; j--)
list.add(list.get(j) | (1 << (i-1))); // XXXXXXX should be |
}
returnlist;
}
}