添加了python代码(数组篇)

为数组篇文件夹下的代码增加了python语言版本
This commit is contained in:
goodyong
2021-07-10 12:20:02 +08:00
parent 4e661354d4
commit 7dd5ce1f3d
13 changed files with 461 additions and 3 deletions

View File

@@ -46,6 +46,8 @@
题目代码
Java Code:
```java
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
@@ -83,5 +85,40 @@ class Solution {
```
Python3 Code:
```python
from typing import List
class Solution:
def spiralOrder(self, matrix: List[List[int]])->List[int]:
arr = []
left = 0
right = len(matrix[0]) - 1
top = 0
down = len(matrix) - 1
while True:
for i in range(left, right + 1):
arr.append(matrix[top][i])
top += 1
if top > down:
break
for i in range(top, down + 1):
arr.append(matrix[i][right])
right -= 1
if left > right:
break
for i in range(right, left - 1, -1):
arr.append(matrix[down][i])
down -= 1
if top > down:
break
for i in range(down, top - 1, -1):
arr.append(matrix[i][left])
left += 1
if left > right:
break
return arr
```