Skip to content

Commit cde20a8

Browse files
adding 1.8
1 parent 85602f7 commit cde20a8

File tree

1 file changed

+53
-7
lines changed

1 file changed

+53
-7
lines changed

Python/chapter01/1.8 - Zero Matrix/camilo_solution_1.8.py

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,67 @@
22
Zero Matrix: Write an algorithm such that if an element in an MxN matrix is 0,
33
its entire row and column are set to 0.
44
Hints:#17, #74, #702
5+
6+
7+
8+
I've been stuck on this for a couple of days..
9+
Really good resource:
10+
https://www.youtube.com/watch?v=ZzpJgRvqSJQ&t=113s
11+
https://www.youtube.com/watch?v=qWeNXOCff3o&t=408s
512
"""
613

714

815

916
def zeroMatrix(matrix):
10-
m = len(matrix) - 1
11-
n = len(matrix[0]) - 1
12-
for i in range(m):
13-
for j in range(n):
14-
if matrix[i][j] == 0:
15-
matrix[i+1][j] = 0
17+
rowZero = False
18+
colZero = False
19+
20+
for i in range(0,len(matrix)):
21+
if matrix[i][0] == 0:
22+
colZero = True
23+
24+
for i in range(0,len(matrix[0])):
25+
if matrix[0][i] == 0:
26+
rowZero = True
27+
28+
for i in range(1,len(matrix)):
29+
for j in range(1,len(matrix[0])):
30+
if matrix[i][j] == 0:
31+
matrix[0][j] == 0
32+
matrix[i][0] == 0
33+
34+
35+
for i in range(1,len(matrix)):
36+
if(matrix[i][0] == 0):
37+
for j in range(1,len(matrix[0])):
38+
matrix[i][j] = 0
39+
40+
for i in range(1,len(matrix[0])):
41+
if(matrix[0][i] == 0):
42+
for j in range(1,len(matrix)):
43+
matrix[i][j] = 0
44+
45+
if rowZero:
46+
for i in range(0,len(matrix[0])):
47+
matrix[0][i] = 0
1648

49+
if colZero:
50+
for i in range(0,len(matrix)):
51+
matrix[i][0] = 0
1752

18-
matrix = [[0,1,2],[3,4,5],[6,7,8]]
1953

2054

55+
56+
57+
58+
59+
60+
61+
matrix = [[0,1,1],
62+
[1,1,1],
63+
[1,1,1]]
64+
2165
zeroMatrix(matrix)
66+
67+
2268
print(matrix)

0 commit comments

Comments
 (0)