-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGray Code.cpp
More file actions
31 lines (29 loc) · 760 Bytes
/
Gray Code.cpp
File metadata and controls
31 lines (29 loc) · 760 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
28
29
30
31
class Solution {
public:
vector<int> grayCode(int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<int> result;
if(n<0)
return result;
else if(n == 0)
{
result.push_back(0);
}else if(n > 0)
{
result.push_back(0);
result.push_back(1);
int sum = 1;
for(int i=1; i<n; i++)
{
sum = sum * 2;
int length = result.size();
for(int j=length - 1; j>=0; j--)
{
result.push_back(result[j]+sum);
}
}
}
return result;
}
};