-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLC_89_GrayCode.cpp
46 lines (37 loc) · 1.04 KB
/
LC_89_GrayCode.cpp
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
https://leetcode.com/problems/gray-code/
89. Gray Code
*/
class Solution {
public:
/*
vector<int> grayCode(int n) {
// Using mirroring concept.
vector<int> ans{0,1};
if(n==1) return ans;
// if(n==2) return {0,1,3,2};
int k=2; // from where we have to mirror
for(int i=2; i<=n; i++) // number of iterations i=1-2-3-4 to n
{
for(int j=k-1; j>=0; j--)
{
ans.push_back(k + ans[j]);
}
k = 2*k;
}
return ans;
}//end
*/
vector<int> grayCode(int n) {
int totRange = 1<<n;
vector<int> ans(totRange); //if n=3 then 8 values (0 to 7)
ans[0] = 0;
ans[1] = 1;
// for(int k = 2; k < totRange; k<<=1)
// for(int j = 0; j < k; j++)
// ans[j+k] = k + ans[k - 1 - j];
for (int i = 0; i < totRange; i++)
ans[i] = (i ^ (i >> 1));
return ans;
}
};