-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path1238.cpp
More file actions
38 lines (38 loc) · 1.18 KB
/
1238.cpp
File metadata and controls
38 lines (38 loc) · 1.18 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
__________________________________________________________________________________________________
sample 156 ms submission
class Solution {
public:
vector<int> circularPermutation(int n, int start) {
vector<int>v;
vector<int>ans;
for (int i = 0; i < (1 << n); i++) {
v.push_back((i ^ (i >> 1)));
}
int inx, flag = 0;
for (int i = 0; i < v.size(); i++)
{
if (v[i] == start) { flag = 1; inx = i; }
if (flag == 1)ans.push_back(v[i]);
}
for (int i = 0; i < inx; i++) {
ans.push_back(v[i]);
}
return ans;
}
};
__________________________________________________________________________________________________
sample 164 ms submission
class Solution {
public:
vector<int> circularPermutation(int n, int start) {
vector<int> ans;
int x = 0;
while ((x ^ (x >> 1)) != start) ++x;
for (int i = 0; i < (1 << n); ++i) {
ans.push_back(x ^ (x >> 1));
x = (x + 1) & ((1 << n) - 1);
}
return ans;
}
};
__________________________________________________________________________________________________