-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path189.cpp
More file actions
33 lines (25 loc) · 767 Bytes
/
189.cpp
File metadata and controls
33 lines (25 loc) · 767 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
// 189. Rotate Array - https://leetcode.com/problems/rotate-array
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
vector<int> rotate(vector<int>& nums, int k) {
int n = (int)nums.size();
k = k % n;
reverse(nums.begin(), nums.begin() + n - k);
reverse(nums.begin() + n - k, nums.end());
reverse(nums.rbegin(), nums.rend());
return nums;
}
};
int main() {
ios::sync_with_stdio(false);
Solution solution;
vector<int> input = {1, 2, 3, 4, 5, 6, 7};
vector<int> expected_ans = {5, 6, 7, 1, 2, 3, 4};
assert(solution.rotate(input, 3) == expected_ans);
input = {-1};
expected_ans = {-1};
assert(solution.rotate(input, 2) == expected_ans);
return 0;
}