forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main3.cpp
53 lines (42 loc) · 1.01 KB
/
main3.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/// Source : https://leetcode.com/problems/rotate-array/description/
/// Author : liuyubobobo
/// Time : 2018-08-10
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
/// Using O(1) Space but O(n^2) operations
/// Time Complexity: O(n^2)
/// Space Complexity: O(1)
class Solution {
public:
void rotate(vector<int>& nums, int k) {
while(k --){
int t = nums.back();
for(int i = (int)nums.size() - 2; i >= 0; i --)
nums[i + 1] = nums[i];
nums[0] = t;
}
return;
}
};
void print_vec(const vector<int>& vec){
for(int e: vec)
cout << e << " ";
cout << endl;
}
int main() {
vector<int> nums1 = {1, 2, 3, 4, 5, 6, 7};
Solution().rotate(nums1, 3);
print_vec(nums1);
// 5 6 7 1 2 3 4
vector<int> nums2 = {1, 2};
Solution().rotate(nums2, 3);
print_vec(nums2);
// 2 1
vector<int> nums3 = {1, 2};
Solution().rotate(nums3, 2);
print_vec(nums3);
// 1 2
return 0;
}