```
class Solution {
public:
vector<int> grayCode(int n) {
vector<int> res{0};
for (int i = 1; i <= n; i++)
{
int mask = 1 << (i - 1);
for (int j = res.size() - 1; j >= 0; j--)
res.push_back(res[j]+mask);
}
return res;
}
};
```