-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
42 lines (41 loc) · 857 Bytes
/
Copy pathmain.cpp
File metadata and controls
42 lines (41 loc) · 857 Bytes
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
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
// 转置3次
class Solution {
public:
void rotate(vector<int>& nums, int k) {
int n = nums.size();
k = k % n;
reverse(nums.begin(), nums.end()-k);
reverse(nums.begin()+n-k, nums.end());
reverse(nums.begin(), nums.end());
}
};
// 利用数组去储存
/*
class Solution {
public:
void rotate(vector<int>& nums, int k) {
vector<int> res(nums.begin(),nums.end());
int n = nums.size();
for(int i = 0; i < nums.size(); ++i)
{
nums[(i+k)%n] = res[i];
}
}
};
*/
int main()
{
vector<int> nums = {1,2,3,4,5,6,7};
Solution s;
s.rotate(nums, 3);
for(int i = 0; i < nums.size(); ++i)
{
cout << nums[i] << endl;
}
return 0;
}