forked from codedecks-in/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path189.Rotate-array.cpp
32 lines (29 loc) · 866 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
difficulty level: Medium
class Solution {
public:
void rotate(vector<int>& nums, int k) {
int n = nums.size();
k = k % n;
// we first reverse all the elements,[7 6 5 4 3 2 1]
for (int i = 0, j = n - 1; i < j; i++, j--) {
swap(nums[i], nums[j]);
}
// then we reverse the set of elements sized k for example [7 6 5 4 3 2 1] = [ 5 6 7 4 3 2 1]
for (int i = 0, j = k - 1; i < j; i++, j--) {
swap(nums[i], nums[j]);
}
//finally we reverse the ending elements too = 5 6 7 1 2 3 4
for (int i = k, j = n - 1; i < j; i++, j--) {
swap(nums[i], nums[j]);
}
}
};
Time complexity:O(n)
Space Complexity:O(1)
/*
Input: nums = [1,2,3,4,5,6,7], k = 3
Output: [5,6,7,1,2,3,4]
Example 2:
Input: nums = [-1,-100,3,99], k = 2
Output: [3,99,-1,-100]
*/