forked from TonnyL/Windary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RotateArray.js
53 lines (48 loc) · 1.13 KB
/
RotateArray.js
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
/**
* Rotate an array of n elements to the right by k steps.
*
* For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].
*
* Accepted.
*/
/**
* @param {number[]} nums
* @param {number} k
* @return {void} Do not return anything, modify nums in-place instead.
*/
let rotate = function (nums, k) {
k %= nums.length;
reverse(nums, 0, nums.length - 1);
reverse(nums, 0, k - 1);
reverse(nums, k, nums.length - 1);
};
let reverse = function (nums, start, end) {
while (start < end) {
let temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}
};
let array0 = [1];
rotate(array0, 1);
if (array0.toString() === [1].toString()) {
console.log("pass")
} else {
console.error("failed")
}
let array1 = [1, 2];
rotate(array1, 1);
if (array1.toString() === [2, 1].toString()) {
console.log("pass")
} else {
console.error("failed")
}
let array2 = [1, 2, 3, 4, 5, 6, 7];
rotate(array2, 3);
if (array2.toString() === [5, 6, 7, 1, 2, 3, 4].toString()) {
console.log("pass")
} else {
console.error("failed")
}