From 7f1c040b282aed31f7c4e6d97721daa61c603c11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E4=BA=9A=E6=9D=B0?= <105789281+zyj1112@users.noreply.github.com> Date: Wed, 11 Oct 2023 15:42:12 +0800 Subject: [PATCH] =?UTF-8?q?Update=200054.=E8=9E=BA=E6=97=8B=E7=9F=A9?= =?UTF-8?q?=E9=98=B5.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0054.螺旋矩阵.md | 54 +++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/problems/0054.螺旋矩阵.md b/problems/0054.螺旋矩阵.md index c62eb2b1..59771a67 100644 --- a/problems/0054.螺旋矩阵.md +++ b/problems/0054.螺旋矩阵.md @@ -251,6 +251,60 @@ var spiralOrder = function(matrix) { return arr }; ``` +### Python + +```python +class Solution(object): + def spiralOrder(self, matrix): + """ + :type matrix: List[List[int]] + :rtype: List[int] + """ + if len(matrix)==0 or len(matrix[0])==0 : # 判定List是否为空 + return [] + row, col = len(matrix), len(matrix[0]) # 行数,列数 + loop = min(row, col) // 2 # 循环轮数 + stx, sty = 0, 0 # 起始x,y坐标 + i, j =0, 0 + count = 0 # 计数 + offset = 1 # 每轮减少的格子数 + result = [0]*(row*col) + while loop>0 :# 左闭右开 + i, j = stx, sty + while j < col - offset : # 从左到右 + result[count] = matrix[i][j] + count += 1 + j += 1 + while i < row - offset : # 从上到下 + result[count] = matrix[i][j] + count += 1 + i += 1 + while j>sty : # 从右到左 + result[count] = matrix[i][j] + count += 1 + j -= 1 + while i>stx : # 从下到上 + result[count] = matrix[i][j] + count += 1 + i -= 1 + stx += 1 + sty += 1 + offset += 1 + loop -= 1 + if min(row, col) % 2 == 1 : # 判定是否需要填充多出来的一行 + i = stx + if row < col : + while i < stx + col - row + 1 : + result[count] = matrix[stx][i] + count += 1 + i += 1 + else : + while i < stx + row - col + 1 : + result[count] = matrix[i][stx] + count += 1 + i += 1 + return result +```