1
+ ##==================================
2
+ ## Leetcode
3
+ ## Student: Vandit Jyotindra Gajjar
4
+ ## Year: 2020
5
+ ## Problem: 73
6
+ ## Problem Name: Set Matrix Zeroes
7
+ ##===================================
8
+ #Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place.
9
+ #
10
+ #Example 1:
11
+ #
12
+ #Input:
13
+ #[
14
+ # [1,1,1],
15
+ # [1,0,1],
16
+ # [1,1,1]
17
+ #]
18
+ #Output:
19
+ #[
20
+ # [1,0,1],
21
+ # [0,0,0],
22
+ # [1,0,1]
23
+ #]
24
+ #Example 2:
25
+ #
26
+ #Input:
27
+ #[
28
+ # [0,1,2,0],
29
+ # [3,4,5,2],
30
+ # [1,3,1,5]
31
+ #]
32
+ #Output:
33
+ #[
34
+ # [0,0,0,0],
35
+ # [0,4,5,0],
36
+ # [0,3,1,0]
37
+ #]
38
+ class Solution :
39
+ def setZeroes (self , matrix ):
40
+ i , j = len (matrix ), len (matrix [0 ]) #Initialize i, j
41
+ c = False #Initialize bool c
42
+ for row in range (i ): #Loop through rows
43
+ if matrix [row ][0 ] == 0 : #Condition-check: If row contains any zero element
44
+ c = True #Update c to True
45
+ for column in range (1 , j ): #Loop through column
46
+ if matrix [row ][column ] == 0 : #Condition-check: If row and column has zero element
47
+ matrix [row ][0 ] = 0 #Set row to zero
48
+ matrix [column ][0 ] = 0 #Set column to zero
49
+ for row in range (1 , i ): #Loop through row
50
+ for column in range (1 , j ): #Loop through column
51
+ if not matrix [row ][0 ] or not matrix [0 ][column ]: #Condition-check: If row and matrix is empty
52
+ matrix [row ][column ] = 0 #Set row and column to zero
53
+ if matrix [0 ][0 ] == 0 : #Condition-check: If 0th element row and column wise found
54
+ for column in range (j ): #Loop through column
55
+ matrix [0 ][column ] = 0 : #set column to zero
56
+ if c : #Condition-check: If False
57
+ for row in range (i ): #Loop through row
58
+ matrix [row ][0 ] = 0 #Set row to zero
0 commit comments