Skip to content

Commit 34478e7

Browse files
committed
feat: spiral-matrix
1 parent 55cf613 commit 34478e7

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

spiral-matrix/HodaeSsi.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# 시간 복잡도 : O(m * n)
2+
# 공간 복잡도 : O(1)
3+
# 문제 유형 : 구현
4+
class Solution:
5+
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
6+
row, col = len(matrix), len(matrix[0])
7+
up, down, left, right = 0, row - 1, 0, col - 1
8+
result = []
9+
while True:
10+
for i in range(left, right + 1):
11+
result.append(matrix[up][i])
12+
up += 1
13+
if up > down:
14+
break
15+
for i in range(up, down + 1):
16+
result.append(matrix[i][right])
17+
right -= 1
18+
if right < left:
19+
break
20+
for i in range(right, left - 1, -1):
21+
result.append(matrix[down][i])
22+
down -= 1
23+
if down < up:
24+
break
25+
for i in range(down, up - 1, -1):
26+
result.append(matrix[i][left])
27+
left += 1
28+
if left > right:
29+
break
30+
return result
31+

0 commit comments

Comments
 (0)