添加0189.旋转数组 js版本

This commit is contained in:
koevas1226
2021-08-24 20:10:20 +08:00
parent c5c95f2aa6
commit 8cfce4eefa

View File

@ -131,6 +131,22 @@ class Solution:
## JavaScript
```js
var rotate = function (nums, k) {
function reverse(nums, i, j) {
while (i < j) {
[nums[i],nums[j]] = [nums[j],nums[i]]; // 解构赋值
i++;
j--;
}
}
let n = nums.length;
k %= n;
if (k) {
reverse(nums, 0, n - 1);
reverse(nums, 0, k - 1);
reverse(nums, k, n - 1);
}
};
```
-----------------------