-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path189.rotate-array.cpp
More file actions
35 lines (33 loc) · 834 Bytes
/
189.rotate-array.cpp
File metadata and controls
35 lines (33 loc) · 834 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
34
35
/*
* @lc app=leetcode id=189 lang=cpp
*
* [189] Rotate Array
*/
// @lc code=start
class Solution
{
public:
// void rotate(vector<int> &nums, int k)
// {
// int size = nums.size();
// if (size==0 or k % size == 0)
// return;
// k = k % size;
// nums.resize(size+k);
// for (int i = size-1; i >= 0; i--)
// nums[i+k] = nums[i];
// for (int i = 0; i < k; i++)
// nums[i] = nums[i + size];
// nums.resize(size);
// }
void rotate(vector<int> &nums, int k)
{
if (nums.empty() or k % nums.size() == 0)
return;
k = k % nums.size();
reverse(nums.begin(), nums.end());
reverse(nums.begin(), nums.begin() + k);
reverse(nums.begin() + k, nums.end());
}
};
// @lc code=end