mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2025-08-03 22:41:58 +00:00
添加了python代码(数组篇)
为数组篇文件夹下的代码增加了python语言版本
This commit is contained in:
@@ -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
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user