Skip to content

Commit f73c2ce

Browse files
committed
set-matrix-zeroes solution
1 parent 60d0ed2 commit f73c2ce

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution:
2+
def setZeroes(self, matrix: List[List[int]]) -> None:
3+
"""
4+
Do not return anything, modify matrix in-place instead.
5+
"""
6+
# 0์ด ์žˆ๋Š” ํ–‰, ์—ด์„ ๊ธฐ๋กํ•ด๋‘๊ณ  ๊ทธ ๊ธฐ๋ก์„ ํ† ๋Œ€๋กœ ๋ฐ”๊พธ๊ธฐ(์‹œ๊ฐ„๋ณต์žก๋„ O(m*n), ๊ณต๊ฐ„๋ณต์žก๋„ O(m+n))
7+
m, n = len(matrix), len(matrix[0])
8+
# rows, cols = [] , [] -> ์ค‘๋ณต๋˜์–ด ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ์Œ. ๋ถˆํ•„์š”ํ•จ. ์ค‘๋ณต์ œ๊ฑฐ๋ฅผ ์œ„ํ•ด set ์‚ฌ์šฉ.
9+
rows, cols = set(), set()
10+
11+
# 0์ด ์žˆ๋Š” ํ–‰, ์—ด ๊ธฐ๋ก
12+
for i in range(m):
13+
for j in range(n):
14+
if matrix[i][j] == 0:
15+
rows.add(i)
16+
cols.add(j)
17+
18+
# ๊ธฐ๋ก๋œ ํ–‰ 0์œผ๋กœ ๋ฐ”๊พธ๊ธฐ
19+
for i in rows:
20+
for j in range(n):
21+
matrix[i][j] = 0
22+
23+
# ๊ธฐ๋ก๋œ ์—ด 0์œผ๋กœ ๋ฐ”๊พธ๊ธฐ
24+
for j in cols:
25+
for i in range(m):
26+
matrix[i][j] = 0

0 commit comments

Comments
ย (0)