Skip to content

Commit 6c3371a

Browse files
committed
update
1 parent e233944 commit 6c3371a

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

18. 旋转图像.md

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
**给定一个 n × n 的二维矩阵 matrix 表示一个图像。请你将图像顺时针旋转 90 度。你必须在 原地 旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要 使用另一个矩阵来旋转图像。**
2+
3+
![algo8](./images/alg8.jpg)
4+
5+
```
6+
输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
7+
输出:[[7,4,1],[8,5,2],[9,6,3]]
8+
```
9+
10+
```
11+
class Solution:
12+
def rotate(self, matrix: List[List[int]]) -> None:
13+
"""
14+
Do not return anything, modify matrix in-place instead.
15+
"""
16+
        n = len(matrix)
17+
        #水平翻转
18+
        for i in range(n//2):
19+
            for j in range(n):
20+
                matrix[i][j], matrix[n-i-1][j] = matrix[n-i-1][j],  matrix[i][j]
21+
        #对角线翻转
22+
        for i in range(n):
23+
            for j in range(i):
24+
                matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
25+
```
26+
27+
```
28+
class Solution:
29+
def rotate(self, matrix: List[List[int]]) -> None:
30+
"""
31+
Do not return anything, modify matrix in-place instead.
32+
"""
33+
matrix[:] = list(map(lambda x: x[::-1], zip(*matrix)))
34+
```

19. 螺旋矩阵.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
**给你一个 m 行 n 列的矩阵 matrix ,请按照顺时针螺旋顺序 ,返回矩阵中的所有元素。**
2+
3+
![algo8](./images/alg8.jpg)
4+
5+
```
6+
输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
7+
输出:[1,2,3,6,9,8,7,4,5]
8+
```
9+
10+
```
11+
class Solution:
12+
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
13+
res = []
14+
while matrix:
15+
#取出第一行
16+
res+=matrix.pop(0)
17+
#剩余矩阵逆时针翻转
18+
matrix = list(zip(*matrix))[::-1]
19+
return res
20+
```

images/algo8.jpg

12.6 KB
Loading

images/algo9.jpg

9.25 KB
Loading

0 commit comments

Comments
 (0)