Skip to content

Commit 39f82b3

Browse files
committed
set matrix zeroes
1 parent 908440e commit 39f82b3

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

set-matrix-zeroes/jungsiroo.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
7+
"""
8+
# Naive change : save rows and cols
9+
# SC : O(m+n)
10+
row_zero, col_zero = set(), set()
11+
12+
rows, cols = len(matrix), len(matrix[0])
13+
for i in range(rows):
14+
for j in range(cols):
15+
if matrix[i][j] == 0:
16+
row_zero.add(i)
17+
col_zero.add(j)
18+
19+
for i in range(rows):
20+
for j in range(cols):
21+
if i in row_zero or j in col_zero:
22+
matrix[i][j] = 0
23+
"""
24+
25+
# Constant Space Complexity using bitmasking
26+
# 0인 구간을 toggle 시켜놓고 확인하는 방법
27+
def is_on(number, k):
28+
return (number & (1<<k)) != 0
29+
30+
row_zero, col_zero = 0, 0
31+
32+
rows, cols = len(matrix), len(matrix[0])
33+
for i in range(rows):
34+
for j in range(cols):
35+
if matrix[i][j] == 0:
36+
row_zero |= (1 << i)
37+
col_zero |= (1 << j)
38+
39+
for i in range(rows):
40+
for j in range(cols):
41+
if is_on(row_zero, i) or is_on(col_zero, j):
42+
matrix[i][j] = 0
43+

0 commit comments

Comments
 (0)