-
Notifications
You must be signed in to change notification settings - Fork 25
/
0189-rotate-array.js
59 lines (45 loc) · 1.39 KB
/
0189-rotate-array.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
54
55
56
57
58
59
// 189. Rotate Array
// Easy 24%
// 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].
// Note:
// Try to come up as many solutions as you can, there are at least 3 different
// ways to solve this problem.
// [show hint]
// Hint:
// Could you do it in-place with O(1) extra space?
// Related problem: Reverse Words in a String II
// Credits:Special thanks to @Freezen for adding this problem and creating all
// test cases.
/**
* @param {number[]} nums
* @param {number} k
* @return {void} Do not return anything, modify nums in-place instead.
*/
const rotate = function(nums, k) {
// js-array-features
// k = k % nums.length
// for (let i = 0; i < k; i++) nums.unshift(nums.pop())
const n = nums.length
k = k % n
for (let j = 0; j < k; j++) {
const num = nums[n - 1]
for (let i = n - 1; i > 0; i--) {
nums[i] = nums[i - 1]
}
nums[0] = num
}
}
;[
[[1], 0], // [1]
[[1,2,3,4], 3], // [2,3,4,1]
[[1,2,3,4,5,6,7], 3], // [5,6,7,1,2,3,4]
[[1,2,3,4,5,6,7,8,9,0], 3] // [8,9,0,1,2,3,4,5,6,7]
].forEach(args => {
console.log(rotate(...args))
})
// Solution:
// 迭代k次,每次将最后一位移到最前面。
// 或者作为一个队列,弹出最后一个,并弹入最前面。
// Submission Result: Accepted