```
class Solution {
public:
vector<int> grayCode(int n) {
int index = 1;
vector<int> result;
result.push_back(0);
if(n == 0) return result;
result.push_back(1);
for (int i = 1; i < n; i++) {
while(index != -1) result.push_back((1<<i) | result[index--]);
index = result.size()-1;
}
return result;
}
};
```