-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotateKsteps.cpp
More file actions
89 lines (71 loc) · 1.51 KB
/
RotateKsteps.cpp
File metadata and controls
89 lines (71 loc) · 1.51 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
82
83
84
85
86
87
88
89
/* Given an array, rotate the array to the right by k steps, where k is non-negative.
Follow up:
Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
Could you do it in-place with O(1) extra space? */
#include<bits/stdc++.h>
using namespace std;
void rotate1(vector<int>& nums, int k) {
k=k%nums.size();
for(int i=0; i<k; i++){
int x=nums[nums.size()-1];
for(int j=nums.size()-1; j>0; j--){
nums[j]= nums[j-1];
}
nums[0]=x;
}
for (int i = 0; i < nums.size(); i++)
{
cout<<nums[i];
}
cout<<endl;
}
void rotate2(vector<int>& nums, int k){
int x=nums.size()-k%nums.size();
for(int i=0; i<x; i++){
nums.push_back(nums[i]);
}
nums.erase(nums.begin(), nums.begin()+x);
for (int i = 0; i < nums.size(); i++)
{
cout<<nums[i];
}
cout<<endl;
}
void reverse(vector<int>& nums, int start, int end)
{
while(start<end)
{
int temp=nums[start];
nums[start]=nums[end];
nums[end]=temp;
start++;
end--;
}
}
void rotate3(vector<int>& nums, int k)
{
k=k%nums.size();
reverse(nums,0, nums.size()-1);
reverse(nums,0,k-1);
reverse(nums,k, nums.size()-1);
for (int i = 0; i < nums.size(); i++)
{
cout<<nums[i];
}
cout<<endl;
}
int main(){
vector<int> nums;
int k,size,x;
cin>>k;
cin>>size;
for (int i = 0; i < size; i++)
{
cin>>x;
nums.push_back(x);
}
rotate1(nums, k);
rotate2(nums, k);
rotate3(nums, k);
return 0;
}