Skip to content

Commit 7d61dc3

Browse files
committed
solve : spiral-matrix/samthekorean.py
1 parent 743028c commit 7d61dc3

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

spiral-matrix/samthekorean.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# TC: O(m * n)
2+
# SC: O(m * n)
3+
# where m is the number of the row and n is the number of columns
4+
class Solution:
5+
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
6+
rows, cols = len(matrix), len(matrix[0])
7+
8+
row, col = 0, -1
9+
10+
direction = 1
11+
12+
result = []
13+
14+
while rows > 0 and cols > 0:
15+
for _ in range(cols):
16+
col += direction
17+
result.append(matrix[row][col])
18+
rows -= 1
19+
20+
for _ in range(rows):
21+
row += direction
22+
result.append(matrix[row][col])
23+
cols -= 1
24+
25+
direction *= -1
26+
27+
return result

0 commit comments

Comments
 (0)