Merge pull request #648 from koevas1226/master

添加0189.旋转数组 js版本
This commit is contained in:
程序员Carl
2021-08-26 11:15:33 +08:00
committed by GitHub

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);
}
};
```
-----------------------