The basic idea is following:

if n = 3

000

001

011

010

100

101

111

110

Look the columns, it is a repeat of {0, 1, 1, 0}. The first column (from the right) is 0110, the send column is 00111100, every element repeat twice, the third column is 0000111111110000, every element repeat four times.

```
class Solution {
public:
vector<int> grayCode(int n) {
int num = 1 << n;
int sum = 0;
vector<int> res;
int t[] = {0, 1, 1, 0};
for (int i = 0; i < num; ++i) {
sum = 0;
for (int j = 0; j < n; ++j) {
sum += t[i/(1 << j) % 4] * (1 << j);
}
res.push_back(sum);
}
return res;
}
};
```