Skip to content

Commit 0c16934

Browse files
committed
Solve: Set Matrix Zeroes
1 parent 67d6e22 commit 0c16934

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
Constraints:
3+
- m == matrix.length
4+
- n == matrix[0].length
5+
- 1 <= m, n <= 200
6+
- -2^31 <= matrix[i][j] <= 2^31 - 1
7+
8+
Time Complexity: O(m*n)
9+
- m์€ ํ–‰, n์€ ์—ด์„ ์˜๋ฏธ
10+
- 0 ์ฐพ๊ธฐ - O(m*n)
11+
- ํ–‰๊ณผ ์—ด ๋ณ€ํ™˜ - O(m*n)
12+
13+
Space Complexity: O(m*n)
14+
- zeros ๋ฐฐ์—ด์ด ์ตœ๋Œ€ m*n ํฌ๊ธฐ๊นŒ์ง€ ์ €์žฅ ๊ฐ€๋Šฅ
15+
16+
ํ’€์ด ๋ฐฉ๋ฒ•:
17+
1. 0 ์œ„์น˜ ์ €์žฅ
18+
2. ์ €์žฅ๋œ 0์˜ ํ–‰๊ณผ ์—ด์„ ๋ชจ๋‘ 0์œผ๋กœ ๋ณ€ํ™˜
19+
3. ์ฃผ์˜์  - ํ–‰๋ ฌ ๊ฐ’ ํƒ์ƒ‰๊ณผ ๋ณ€๊ฒฝ์„ ๋™์‹œ์— ์ˆ˜ํ–‰ํ•˜๋ฉด ์›๋ž˜ ์–ด๋–ค ๊ฐ’์ด 0์ด์—ˆ๋Š”์ง€ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์–ด๋ ค์›Œ์ง
20+
"""
21+
22+
class Solution:
23+
def setZeroes(self, matrix: List[List[int]]) -> None:
24+
"""
25+
Do not return anything, modify matrix in-place instead.
26+
"""
27+
zeros = []
28+
29+
for r in range(len(matrix)):
30+
for c in range(len(matrix[0])):
31+
if matrix[r][c] == 0:
32+
zeros.append((r, c))
33+
34+
for r, c in zeros:
35+
for i in range(len(matrix[0])):
36+
matrix[r][i] = 0
37+
for i in range(len(matrix)):
38+
matrix[i][c] = 0

0 commit comments

Comments
ย (0)