class Solution {
public:
vector<int> grayCode(int n) {
vector<int> res;
res.push_back(0);long pow2 = 1;
for (int i=1; i <= n; i++) {
int old_len = res.size();
for (int i=0; i<old_len; i++) {
res.push_back(pow2 + res[old_len - i - 1]);
}
pow2<<=1;
}
return res;
}
};
再水一发, 不过n==0时,觉得应该返回一个空的vector
时间: 2024-10-16 20:44:00