mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
@ -131,6 +131,22 @@ class Solution:
|
|||||||
## JavaScript
|
## JavaScript
|
||||||
|
|
||||||
```js
|
```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);
|
||||||
|
}
|
||||||
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
|
Reference in New Issue
Block a user