-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path189-rotate-array.cpp
More file actions
63 lines (55 loc) · 1.65 KB
/
Copy path189-rotate-array.cpp
File metadata and controls
63 lines (55 loc) · 1.65 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
// 189. Rotate Array
//
// Rotate an array of n elements to the right by k steps.
// For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].
// Note:
// Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
//
// Hint:
// Could you do it in-place with O(1) extra space?
//
// Tags: Array
//
// https://leetcode.com/problems/rotate-array/
#include <iostream>
#include <gtest/gtest.h>
using namespace std;
class Solution {
public:
void rotate(vector<int>& nums, int k) {
vector<int> result(nums.size());
int size = nums.size();
for(int i = 0 ; i < size; i++){
if((i + k) < size) {
result[i + k] = nums[i];
}else{
result[(i + k) % size] = nums[i];
}
}
nums = result;
}
};
TEST(leetcode_189_rotate_array, Basic)
{
Solution *solution = new Solution();
vector<int> nums = {1, 2, 3, 4, 5, 6, 7};
vector<int> expected = {5, 6, 7 ,1 ,2 ,3, 4};
solution->rotate(nums, 3);
EXPECT_EQ(expected, nums);
nums = {1, 2, 3, 4, 5, 6, 7};
expected = {4, 5, 6, 7, 1, 2, 3};
solution->rotate(nums, 4);
EXPECT_EQ(expected, nums);
nums = {1, 2, 3, 4, 5, 6};
expected = {2, 3, 4, 5, 6, 1};
solution->rotate(nums, 11);
EXPECT_EQ(expected, nums);
nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
expected = {10, 11, 12, 13, 1, 2, 3, 4, 5, 6, 7, 8, 9};
solution->rotate(nums, 17);
EXPECT_EQ(expected, nums);
}
int main(int argc, char *argv[]) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}