forked from chihungyu1116/leetcode-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path189 Rotate Array.js
More file actions
30 lines (27 loc) · 675 Bytes
/
Copy path189 Rotate Array.js
File metadata and controls
30 lines (27 loc) · 675 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
// Leetcode #189
// Language: Javascript
// Problem: https://leetcode.com/problems/rotate-array/
// Author: Chihung Yu
/**
* @param {number[]} nums
* @param {number} k
* @return {void} Do not return anything, modify nums in-place instead.
*/
Array.prototype.reverseFromLToR = function(left,right){
if(right >= this.length){
return;
}
while(left < right){
var temp = this[left];
this[left] = this[right];
this[right] = temp;
left++;
right--;
}
}
var rotate = function(nums, k) {
k = k%nums.length;
nums.reverse();
nums.reverseFromLToR(0,k-1);
nums.reverseFromLToR(k,nums.length-1);
};