-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathrotation_game.cpp
More file actions
81 lines (78 loc) · 1.72 KB
/
rotation_game.cpp
File metadata and controls
81 lines (78 loc) · 1.72 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include<iostream>
#include<vector>
using namespace std;
class Solution
{
public:
void reverse(vector<int>& nums, int low, int high)
{
while(low<high)
{
swap(nums[low],nums[high]);
low++;
high--;
}
}
void right(vector<int>& nums, int k)
{
int n = nums.size();
reverse(nums,0,n-k-1);
reverse(nums,n-k,n-1);
reverse(nums,0,n-1);
}
void left(vector<int>& nums, int k)
{
int n = nums.size();
reverse(nums,0,k-1);
reverse(nums,k,n-1);
reverse(nums,0,n-1);
}
void rotate(vector<int>& nums, int k, vector<int> rotations)
{
for(int i=0; i<rotations.size(); i++)
{
if(rotations[i]==1)
{
left(nums,k);
display(nums);
}
else if(rotations[i]==0)
{
right(nums,k);
display(nums);
}
}
}
void display(vector<int> nums)
{
for(auto i : nums)
{
cout<<i<<" ";
}
cout<<endl;
}
};
int main()
{
int numbers[] = {1,2,3,4,5,6,7};
vector<int> nums;
for(auto i : numbers)
{
nums.push_back(i);
}
int rot[] = {1,1,1,0,0,1,1};
vector<int> rotations;
for(auto i : rot)
{
rotations.push_back(i);
}
int k = 3;
Solution s;
s.rotate(nums, k, rotations);
for(auto i : nums)
{
cout<<i<<" ";
}
cout<<endl;
return 0;
}