-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGray Code.txt
More file actions
executable file
·53 lines (52 loc) · 1.03 KB
/
Gray Code.txt
File metadata and controls
executable file
·53 lines (52 loc) · 1.03 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
int k;
vector<string> v;
void solve()
{
if(k<=1)
return;
vector<string>v1(v);
reverse(v1.begin(),v1.end());
for(int i=0;i<v.size();++i)
{
v[i]="0"+v[i];
v1[i]="1"+v1[i];
}
for(int i=0;i<v1.size();++i)
v.push_back(v1[i]);
v1.clear();
--k;
solve();
}
int btoi(string str)
{
int s=0,i=0,p=1;
for(int j=str.length()-1;j>=0;--j)
{
if(str[j]=='1')
{
s+=p;
}
++i;
p<<=1;
}
return s;
}
vector<int> Solution::grayCode(int A) {
// Do not write main() function.
// Do not read input, instead use the arguments to the function.
// Do not print the output, instead return values as specified
// Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details
k=A;
vector<int>arr;
if(A==0)
return arr;
v.clear();
v.push_back("0");
v.push_back("1");
solve();
for(int i=0;i<v.size();++i)
{
arr.push_back(btoi(v[i]));
}
return arr;
}