8000 3 · Dujx/leetcode@6741cda · GitHub
[go: up one dir, main page]

Skip to content

Commit 6741cda

Browse files
committed
3
1 parent 3305d5e commit 6741cda

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

GrayCode/GrayCode.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,21 @@ class Solution {
99
gray_code.push_back((i >> 1) ^ i);
1010
return gray_code;
1111
}
12-
};
12+
};
13+
14+
// More comprehensible solution
15+
class Solution {
16+
public:
17+
vector<int> grayCode(int n) {
18+
vector<int> result(1, 0);
19+
for (int i = 0; i < n; i++) {
20+
int curr = result.size();
21+
while (curr) {
22+
curr--;
23+
int x = result[curr];
24+
result.push_back((1 << i) + x);
25+
}
26+
}
27+
return result;
28+
}
29+
};

0 commit comments

Comments
 (0)
0