-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGray_Code.cpp
More file actions
62 lines (58 loc) · 1.57 KB
/
Gray_Code.cpp
File metadata and controls
62 lines (58 loc) · 1.57 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
the second is anson's, his is better.
A[n]=(n>>1)^n;
as for solution one, it's fomular is not cool
0 1
00 01 11 10
000 001 011 010 110 111 101 100
the reverse...
O(N)
*/
class Solution {
public:
vector<int> grayCode(int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<int> res;
res.push_back(0);
if(n==0)return res;
res.push_back(1);
for(int i=1;i<n;i++)
{
vector<int> tmp;
tmp=res;
for(int j=res.size()-1;j>=0;j--)
{
tmp.push_back(pow(2,i)+res[j]);
}
res=tmp;
}
return res;
}
vector<int> grayCode(int n) {
vector<int> result;
int num = 1 << n;
for (int i = 0; i < num; i++) {
result.push_back((i >> 1) ^ i);
}
return result;
}
int GrayToBinary(int n, int k)
{
int toReturn=0;
vector<int> gray;
for(int i=0;i<k;i++)
{
gray.push_back(n%2);
n/=2;
}
reverse(gray.begin(), gray.end());
int prev=0;
for(int i=0;i<k;i++)
{
prev=(prev+gray[i])%2;
toReturn=toReturn*2+prev;
}
return toReturn;
}
};