class Solution {
public:
vector<int> grayCode(int n) {
vector<int> grayList(1 << n, 0);
/*
if (n == 0) {
return grayList;
}
else {
grayList[0] = 0;
grayList[1] = 1;
}
for (int i = 1; i < n; i++) {
int exp = 1 << i;
for (int j = 0; j < exp; j++) {
grayList[exp + j] = exp | grayList[exp - j - 1];
}
}
return grayList;
*/
for (int i = 0; i < (1 << n); i++) {
grayList[i] = i ^ (i >> 1);
}
return grayList;
}
};
Leetcode 89. 格雷码生成
最新推荐文章于 2025-01-22 09:15:28 发布