-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path189.cpp
74 lines (67 loc) · 1.57 KB
/
189.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <algorithm>
#include <cassert>
#include <gtest/gtest.h>
#include <vector>
using namespace std;
class Solution {
public:
void rotate(vector<int> &nums, int k)
{
int n = nums.size();
k %= n;
vector<int> tmp(n);
for (int i = 0; i < n; i++) {
if (i < k) {
tmp[i] = nums[n - k + i];
}
else {
tmp[i] = nums[i - k];
}
}
nums = tmp;
}
void rotate2(vector<int> &nums, int k)
{
reverse(nums.begin(), nums.end());
reverse(nums.begin(), nums.begin() + k % nums.size());
reverse(nums.begin() + k % nums.size(), nums.end());
}
};
class Testing : public testing::Test {
public:
Solution solution;
void SetUp() {}
void TearDown() {}
};
TEST_F(Testing, Case1)
{
vector<int> nums = {1, 2, 3, 4, 5, 6, 7};
int k = 3;
vector<int> expected = {5, 6, 7, 1, 2, 3, 4};
solution.rotate(nums, k);
EXPECT_EQ(nums, expected);
}
TEST_F(Testing, Case2)
{
vector<int> nums = {-1, -100, 3, 99};
int k = 2;
vector<int> expected = {3, 99, -1, -100};
solution.rotate(nums, k);
EXPECT_EQ(nums, expected);
}
TEST_F(Testing, Case1_2)
{
vector<int> nums = {1, 2, 3, 4, 5, 6, 7};
int k = 3;
vector<int> expected = {5, 6, 7, 1, 2, 3, 4};
solution.rotate2(nums, k);
EXPECT_EQ(nums, expected);
}
TEST_F(Testing, Case2_2)
{
vector<int> nums = {-1, -100, 3, 99};
int k = 2;
vector<int> expected = {3, 99, -1, -100};
solution.rotate2(nums, k);
EXPECT_EQ(nums, expected);
}