From 8a7ea66c6f136ff802dd81411f21381dbcdb357f Mon Sep 17 00:00:00 2001 From: Logen <47022821+Logenleedev@users.noreply.github.com> Date: Thu, 16 Feb 2023 19:19:49 -0600 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=A7=A3=E9=A2=98=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= 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 8a7b0227..35819694 100644 --- a/problems/0189.旋转数组.md +++ b/problems/0189.旋转数组.md @@ -108,6 +108,7 @@ class Solution { ## Python +方法一:局部翻转 + 整体翻转 ```python class Solution: def rotate(self, A: List[int], k: int) -> None: @@ -123,6 +124,21 @@ class Solution: reverse(k, n - 1) ``` +方法二:利用余数 + +```python +class Solution: + def rotate(self, nums: List[int], k: int) -> None: + copy = nums[:] + + for i in range(len(nums)): + nums[(i + k) % len(nums)] = copy[i] + + return nums + + # 备注:这个方法会导致空间复杂度变成 O(n) 因为我们要创建一个 copy 数组。但是不失为一种思路。 +``` + ## Go ```go