-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path189.rotate-array.cpp
More file actions
executable file
·114 lines (97 loc) · 2.88 KB
/
Copy path189.rotate-array.cpp
File metadata and controls
executable file
·114 lines (97 loc) · 2.88 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
@copyright 2004-2015 Apache License, Version 2.0
@filename 189.rotate-array.cpp
@author root
@version
@date 2019/09/27 01:02
@brief
@details 2019/09/27 root create
*/
/*************************************************************************
> File Name: 189.rotate-array.cpp
> Author: hulkcao
> Mail: [email protected]
> Created Time: Sat 03 Aug 2019 03:15:17 AM UTC
************************************************************************/
#include <algorithm>
#include <chrono>
#include <iostream>
#include <list>
#include "common_define.h"
using namespace std;
using namespace chrono;
/*
Given an array, rotate the array to the right by k steps, where k is non-negative.
Example 1:
Input: [1,2,3,4,5,6,7] and k = 3
Output: [5,6,7,1,2,3,4]
Explanation:
rotate 1 steps to the right: [7,1,2,3,4,5,6]
rotate 2 steps to the right: [6,7,1,2,3,4,5]
rotate 3 steps to the right: [5,6,7,1,2,3,4]
Example 2:
Input: [-1,-100,3,99] and k = 2
Output: [3,99,-1,-100]
Explanation:
rotate 1 steps to the right: [99,-1,-100,3]
rotate 2 steps to the right: [3,99,-1,-100]
Note:
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?
*/
class Solution
{
public:
// solution 1 buff overflow
void rotate(vector<int> &nums, int k)
{
if (k <= 0) {
return;
}
std::list<int> tmplist;
for (auto &v : nums) {
tmplist.push_back(v);
}
if (tmplist.empty()) {
return;
}
for (int i = 0; i < k; i++) {
int t = tmplist.back();
tmplist.pop_back();
tmplist.push_front(t);
}
nums.clear();
for (auto &v : tmplist) {
nums.push_back(v);
}
}
void rotate2(vector<int> &nums, int k)
{
int n = nums.size();
k = k % n;
// Reverse the first n - k numbers.
// Index i (0 <= i < n - k) becomes n - k - i.
std::reverse(nums.begin(), nums.begin() + n - k);
// Reverse tha last k numbers.
// Index n - k + i (0 <= i < k) becomes n - i.
std::reverse(nums.begin() + n - k, nums.begin() + n);
// Reverse all the numbers.
// Index i (0 <= i < n - k) becomes n - (n - k - i) = i + k.
// Index n - k + i (0 <= i < k) becomes n - (n - i) = i.
std::reverse(nums.begin(), nums.begin() + n);
}
};
int main()
{
Solution s;
std::vector<int> arr{1, 2, 3, 4, 5, 6, 7};
for (int i = 0; i < 10000000; i++) {
arr.push_back(i);
}
auto start = system_clock::now();
s.rotate2(arr, 3);
auto end = system_clock::now();
auto duration = duration_cast<microseconds>(end - start);
cout << "cost" << double(duration.count()) * microseconds::period::num / microseconds::period::den << "秒" << endl;
return 0;
}