forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCircular Permutation in Binary Representation.cpp
62 lines (57 loc) · 1.3 KB
/
Circular Permutation in Binary Representation.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Runtime: 280 ms (Top 7.77%) | Memory: 78.8 MB (Top 5.30%)
class Solution {
public:
vector<string> get_val(int n)
{
if(n==1)return {"0","1"};
vector<string> v = get_val(n-1);
vector<string> ans;
for(int i = 0;i<v.size();i++)
{
ans.push_back("0" + v[i]);
}
for(int i = v.size()-1;i>=0;i--)
{
ans.push_back("1" + v[i]);
}
return ans;
}
vector<int> solve(int n)
{
vector<string> v = get_val(n);
vector<int> ans;
for(int i = 0;i<v.size();i++)
{
string s = v[i];
int x = 0;
for(int j = 0;j<s.size();j++)
{
x = x*2 + s[j]-'0';
}
ans.push_back(x);
}
return ans;
}
vector<int> circularPermutation(int n, int start) {
vector<int> v = solve(n);
int ind;
for(int i = 0;i<v.size();i++)
{
if(v[i]==start)
{
ind = i;
break;
}
}
vector<int> ans;
for(int i = ind;i<v.size();i++)
{
ans.push_back(v[i]);
}
for(int i = 0;i<ind;i++)
{
ans.push_back(v[i]);
}
return ans;
}
};