-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path189.cpp
More file actions
66 lines (62 loc) · 1.42 KB
/
189.cpp
File metadata and controls
66 lines (62 loc) · 1.42 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* 189.cpp
*
* Created on: Mar 2, 2015
* Author: zhangzuowei
*/
#include<iostream>
using namespace std;
class Solution {
public:
void rotate(int nums[], int n, int k) {
int step = n - k % n;
if(step == n)
return;
for(int i = 0; i < gcd(n, step); i++) {
int loc = i, shift_loc = i + step, temp = nums[loc];
do {
nums[loc] = nums[shift_loc];
loc = shift_loc;
shift_loc = (shift_loc + step) % n;
} while(shift_loc != i);
nums[loc] = temp;
}
return;
}
private:
static int gcd(int a, int b) {
int big = (a > b) ? a : b;
int small = (a < b) ? a : b;
if(!small) return big;
int r = big % small;
while(r) {
big = small;
small = r;
r = big % small;
}
return small;
}
};
class Test {
public:
void sample() {
int input1[] = {1,2,3,4,5,6,7}, n1 = 7, k1 = 3;
int input2[] = {1,2,3,4,5,6}, n2 = 6, k2 = 0;
int input3[] = {1,2,3}, n3 = 3, k3 = 3;
int input4[] = {1}, n4 = 1, k4 = 10;
int input5[] = {1,2,3,4,5,6}, n5 = 6, k5 = 2;
runTest(input1, n1, k1);
runTest(input2, n2, k2);
runTest(input3, n3, k3);
runTest(input4, n4, k4);
runTest(input5, n5, k5);
}
private:
void runTest(int input[], int n, int k) {
Solution *s = new Solution();
s -> rotate(input, n, k);
for(int i = 0; i < n; i++)
cout << input[i] << ",";
cout << endl;
}
};