Skip to content

Commit f46cec2

Browse files
Add files via upload
1 parent de2c12e commit f46cec2

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Arrays/Rotate Array (LeetCode).cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution
2+
{
3+
public:
4+
void rotate(vector<int>& nums, int k)
5+
{
6+
int n = nums.size();
7+
int start = n - k % n;
8+
int remainder = k % n;
9+
vector<int> v(n);
10+
11+
for(int i = 0; i < remainder; i++)
12+
{
13+
v[i] = nums[i + start];
14+
}
15+
16+
for(int i = 0; i < start; i++)
17+
{
18+
v[i + remainder] = nums[i];
19+
}
20+
21+
for(int i = 0; i < n; i++)
22+
{
23+
nums[i] = v[i];
24+
}
25+
}
26+
};
27+

0 commit comments

Comments
 (0)