We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 42884bc + f3898a5 commit 0e08185Copy full SHA for 0e08185
javascript/0189-rotate-array.js
@@ -0,0 +1,26 @@
1
+/**
2
+ * Two Pointers
3
+ * https://leetcode.com/problems/rotate-array/
4
+ *
5
+ * Time O(n) | Space O(1)
6
+ * @param {number[]} nums
7
+ * @param {number} k
8
+ * @return {void} Do not return anything, modify nums in-place instead.
9
+ */
10
+ var rotate = function(nums, k) {
11
+
12
+ // if the k exceeds the length of nums.
13
+ k = k % nums.length;
14
15
+ nums.reverse();
16
+ reversePortionOfArray(nums, 0, k - 1);
17
+ reversePortionOfArray(nums,k, nums.length - 1);
18
+};
19
20
+var reversePortionOfArray = function(nums,start,end) {
21
+ while(start < end) {
22
+ [nums[start],nums[end]] = [nums[end],nums[start]];
23
+ start++;
24
+ end--;
25
+ }
26
+}
0 commit comments