-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path89.cpp
More file actions
27 lines (26 loc) · 753 Bytes
/
Copy path89.cpp
File metadata and controls
27 lines (26 loc) · 753 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution {
public:
vector<int> grayCode(int n) {
if (n == 0)
return vector<int>(1, 0);
vector<int> grayCode(n, 0);
vector<int> ret;
BackTracking(n, 0, grayCode, ret);
return ret;
}
void BackTracking(int &n, int i, vector<int> &grayCode, vector<int> &ret) {
if (i == n) {
int sum = 0;
int bin = 1;
for (int k = 0; k < grayCode.size(); ++k) {
sum += grayCode[k] * bin;
bin *= 2;
}
ret.push_back(sum);
return;
}
BackTracking(n, i + 1, grayCode, ret);
grayCode[i] = (grayCode[i] == 1 ? 0 : 1);
BackTracking(n, i + 1, grayCode, ret);
}
};