mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 15:45:40 +08:00
添加解题方法
This commit is contained in:
@ -108,6 +108,7 @@ class Solution {
|
|||||||
|
|
||||||
## Python
|
## Python
|
||||||
|
|
||||||
|
方法一:局部翻转 + 整体翻转
|
||||||
```python
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
def rotate(self, A: List[int], k: int) -> None:
|
def rotate(self, A: List[int], k: int) -> None:
|
||||||
@ -123,6 +124,21 @@ class Solution:
|
|||||||
reverse(k, n - 1)
|
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
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
Reference in New Issue
Block a user