문제링크
📢 문제 간략설명
📌풀이 알고리즘
- top, bottom, left, right 변수를 둬서
오른쪽으로 갈 때, 내려갈 때, 왼쪽으로 갈 때, 올라갈 때의
4가지 경우 반복
📌소스코드 및 풀이방법
class Solution:
def spiralOrder(self, matrix):
if not matrix:
return []
res = []
row, col = len(matrix), len(matrix[0])
top = 0
bottom = row-1
left = 0
right = col-1
while top <= bottom and left <= right:
for i in range(left, right+1):
res.append(matrix[top][i])
top += 1
for i in range(top,bottom+1):
res.append(matrix[i][right])
right-=1
if top <= bottom :
for i in range(right,left-1,-1):
res.append(matrix[bottom][i])
bottom-=1
if left <= right:
for i in range(bottom, top-1, -1):
res.append(matrix[i][left])
left +=1
print(res)
return res
문제후기