I have a solution here which takes O(1) on space and no recursion used. Is this the best possible solution? (I combined the base cases in the loop as mike3 does. Thanks mike3!)

```
vector<int> grayCode(int n)
{
vector<int> result(1, 0);
for (int i = 0; i < n; i++) {
int curCount = result.size();
// push back all element in result in reverse order
while (curCount) {
curCount--;
int curNum = result[curCount];
curNum += (1<<i);
result.push_back(curNum);
}
}
return result;
}
```