forked from larissalages/code_problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path189_rotate_array.cpp
More file actions
31 lines (29 loc) · 1.06 KB
/
189_rotate_array.cpp
File metadata and controls
31 lines (29 loc) · 1.06 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
class Solution {
public:
void reverse(vector<int> &nums, int startIndex, int endIndex)
{
//This function reverses the vector for the given start and end Indices of the Vector
while(startIndex < endIndex)
{
int temp = nums[startIndex];
nums[startIndex] = nums[endIndex];
nums[endIndex] = temp;
startIndex++;
endIndex--;
}
}
void rotate(vector<int>& nums, int k)
{
int length = nums.size(); //Stores the Size of the Vector into the length variable
if(k > length)
{
k %= length; //Making Sure that the Rotation is within the length of the Vector
}
if(k == 0 || k == length) //If k is zero or k is equal to the length of the Vector there is no need to perform operations on it
return;
//Rotating the Vector with the help of the Reverse Function
reverse(nums, 0, length - 1);
reverse(nums, 0, k - 1);
reverse(nums, k, length - 1);
}
};