Skip to content

Commit 3a22d80

Browse files
committed
Time: 32 ms (38.53%), Space: 25.1 MB (37.49%) - LeetHub
1 parent 944824d commit 3a22d80

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

rotate-array/rotate-array.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
3+
public:
4+
5+
void reverse(vector<int>& a,int low,int high) {
6+
while(low<=high) {
7+
swap(a[low],a[high]);
8+
low++;
9+
high--;
10+
}
11+
}
12+
void rotate(vector<int>& nums, int k) {
13+
14+
int n = nums.size();
15+
k = k % n;
16+
reverse(nums,0,n-1);
17+
reverse(nums, 0,k-1);
18+
reverse(nums, k, n-1);
19+
20+
}
21+
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy