Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8a7ea66

Browse files
添加解题方法
1 parent 633293b commit 8a7ea66

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

‎problems/0189.旋转数组.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ class Solution {
108108

109109
## Python
110110

111+
方法一:局部翻转 + 整体翻转
111112
```python
112113
class Solution:
113114
def rotate(self, A: List[int], k: int) -> None:
@@ -123,6 +124,21 @@ class Solution:
123124
reverse(k, n - 1)
124125
```
125126

127+
方法二:利用余数
128+
129+
```python
130+
class Solution:
131+
def rotate(self, nums: List[int], k: int) -> None:
132+
copy = nums[:]
133+
134+
for i in range(len(nums)):
135+
nums[(i + k) % len(nums)] = copy[i]
136+
137+
return nums
138+
139+
# 备注:这个方法会导致空间复杂度变成 O(n) 因为我们要创建一个 copy 数组。但是不失为一种思路。
140+
```
141+
126142
## Go
127143

128144
```go

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /