-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path189 Rotate Array.cpp
34 lines (28 loc) · 999 Bytes
/
189 Rotate Array.cpp
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
static int fastio=[](){
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
return 0;
}();
class Solution {
public:
void rotate(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.
reverse(nums.begin(), nums.begin() + n - k);
// Reverse tha last k numbers.
// Index n - k + i (0 <= i < k) becomes n - i.
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.
reverse(nums.begin(), nums.begin() + n);
// Another Approach:
// k %=nums.size();
// reverse(nums.begin(), nums.end());
// reverse(nums.begin(), nums.begin()+k);
// reverse(nums.begin()+k, nums.end());
}
};