Repository navigation
Expand file tree
/
Copy pathRotateArrayTest.java
More file actions
85 lines (67 loc) · 1.53 KB
/
Copy pathRotateArrayTest.java
File metadata and controls
85 lines (67 loc) · 1.53 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package org.overmind;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
/**
* <a href="https://leetcode.com/problems/rotate-array/">189. Rotate Array</a> (Medium)
* <p>
* Rotate the array to the right by {@code k} steps in place.
*/
public class RotateArrayTest {
private final Solution solution = new Solution();
@Test
void example1() {
// given
var nums = new int[]{1, 2, 3, 4, 5, 6, 7};
// when
solution.rotate(nums, 3);
// then
assertThat(nums)
.containsExactly(5, 6, 7, 1, 2, 3, 4);
}
@Test
void example2() {
// given
var nums = new int[]{-1, -100, 3, 99};
// when
solution.rotate(nums, 2);
// then
assertThat(nums)
.containsExactly(3, 99, -1, -100);
}
@Test
void example3() {
// given
var nums = new int[]{1, 2, 3, 4, 5, 6};
// when
solution.rotate(nums, 4);
// then
assertThat(nums)
.containsExactly(3, 4, 5, 6, 1, 2);
}
class Solution {
public void rotate(int[] nums, int k) {
int d = gcd(nums.length, k);
for (int j = 0; j < d; j += 1) {
int tmp = nums[j];
int i = j;
do {
int n = (i + k) % nums.length;
int tmp2 = nums[n];
nums[n] = tmp;
tmp = tmp2;
i = n;
} while (i != j);
}
}
public int gcd(int a, int b) {
while (a > 0 && b > 0) {
if (a > b) {
a = a % b;
} else {
b = b % a;
}
}
return a + b;
}
}
}