forked from azheanda/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraycode.java
More file actions
36 lines (30 loc) · 822 Bytes
/
Copy pathgraycode.java
File metadata and controls
36 lines (30 loc) · 822 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
/*
judge small : passed 516ms
judge larget : passed 596ms
*/
import java.util.ArrayList;
import java.util.Collections;
public class Solution {
public ArrayList<Integer> grayCode(int n) {
// Start typing your Java solution below
// DO NOT write main() function
ArrayList<Integer> res = new ArrayList<Integer>();
ArrayList<Integer> preres = new ArrayList<Integer>();
res.add(0);
if(n==0)
return res;
res.add(1);
if(n==1)
return res;
for(int i=2;i<=n;i++){
for(Integer j:res)
preres.add(j);
Collections.reverse(res);
for(Integer j:res)
preres.add((int)(j+Math.pow(2,i-1)));
res = preres;
preres= new ArrayList<Integer>();
}
return res;
}
}