-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset_matrix_zeroes.py
More file actions
45 lines (38 loc) · 1.04 KB
/
set_matrix_zeroes.py
File metadata and controls
45 lines (38 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place.
"""
>>> matrix = [
... [1, 1, 1],
... [1, 0, 1],
... [1, 1, 1]
... ]
>>> set_zeroes(matrix)
>>> matrix
[[1, 0, 1], [0, 0, 0], [1, 0, 1]]
>>> matrix = [
... [0, 1, 2, 0],
... [3, 4, 5, 2],
... [1, 3, 1, 5],
... ]
>>> set_zeroes(matrix)
>>> matrix
[[0, 0, 0, 0], [0, 4, 5, 0], [0, 3, 1, 0]]
>>> matrix = []
>>> set_zeroes(matrix)
>>> matrix
[]
"""
from typing import List
def set_zeroes(matrix: List[List[int]]) -> None:
mark_zeroes = set()
for i, row in enumerate(matrix):
for j, element in enumerate(row):
if element == 0:
mark_zeroes.add((i, j))
change_matrix(mark_zeroes, matrix)
def change_matrix(mark_zeroes, matrix):
for mark in mark_zeroes:
idx_i, idx_j = mark
for j in range(len(matrix[0])):
matrix[idx_i][j] = 0
for i in range(len(matrix)):
matrix[i][idx_j] = 0