From 8cfce4eefa9df796bf936698d58a7add88dd49ca Mon Sep 17 00:00:00 2001 From: koevas1226 <3130104027@stmail.ujs.edu.cn> Date: Tue, 24 Aug 2021 20:10:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A00189.=E6=97=8B=E8=BD=AC?= =?UTF-8?q?=E6=95=B0=E7=BB=84=20js=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0189.旋转数组.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/problems/0189.旋转数组.md b/problems/0189.旋转数组.md index be46bf4c..a2b2b206 100644 --- a/problems/0189.旋转数组.md +++ b/problems/0189.旋转数组.md @@ -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); + } +}; ``` -----------------------