-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGray_Code_89.java
More file actions
36 lines (27 loc) · 788 Bytes
/
Copy pathGray_Code_89.java
File metadata and controls
36 lines (27 loc) · 788 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
29
30
31
32
33
34
35
36
package Algorithms;
import java.util.ArrayList;
import java.util.List;
public class Gray_Code_89 {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
class Solution_Gray_Code_89 {
public List<Integer> grayCode(int n) {
ArrayList<Integer> set = new ArrayList<Integer>() ;
set.add(0) ;
if(n == 0) return set ;
set.add(1) ;
int posBit = 2 ;
for(int i=1; i<n; i++) {
int len = set.size() ;
System.out.println("size: " + set.size()) ;
for(int j=len-1; j>=0; j--) {
int temp_num = set.get(j) ;
set.add(temp_num|posBit) ;
}
posBit = posBit << 1 ;
}
return set ;
}
}