-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path189.Rotate Array.cpp
More file actions
44 lines (40 loc) · 849 Bytes
/
189.Rotate Array.cpp
File metadata and controls
44 lines (40 loc) · 849 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
36
37
38
39
40
41
42
43
class Solution {
public:
// O(k) 空间
void rotate(vector<int>& nums, int k) {
int n = nums.size();
k = k%n;
// ERROR1: 需要判断k == 0
if(k == 0) return;
vector<int> buffer(k);
for(int j=0; j < k; j++) {
buffer[j] = nums[j];
}
int i = 0, s_idx = 0;
while(i < n) {
s_idx += k;
for(int j=0; j < k; j++, i++) {
std::swap(nums[(s_idx + j)%n], buffer[j]);
}
}
}
// O(1)空间,出过好几次错误
void rotate2(vector<int>& nums, int k) {
int n = nums.size();
int nn = gcd(n, k);
for(int i=0; i<nn; i++) {
int tmp = nums[i];
for(int j=1; j<= n/nn; j++){
int idx = (i + j*k) % n;
// 用swap,这样不需要两个tmp
std::swap(nums[idx], tmp);
}
}
}
private:
int gcd(int a, int b) {
if(a == 0) return b;
if(b == 0) return a;
return gcd(b, a%b);
}
};