File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ def setZeroes(self, matrix):
2+ """
3+ :type matrix: List[List[int]]
4+ :rtype: None Do not return anything, modify matrix in-place instead.
5+ """
6+ first_Row=False
7+ first_Col=False
8+
9+ #Check if first column has any zeroes
10+ for i in range(0,len(matrix)):
11+ if(matrix[i][0] ==0):
12+ first_Col = True
13+
14+ # Check if first row has any zeroes
15+ for i in range(0,len(matrix[0])):
16+ if(matrix[0][i] ==0):
17+ first_Row = True
18+
19+ #Check rest of matrix for zeros,mark the row and column using first row and column
20+ for i in range(1,len(matrix)):
21+ for j in range(1,len(matrix[0])):
22+ if(matrix[i][j] ==0):
23+ matrix[0][j]=0
24+ matrix[i][0]=0
25+
26+ #Zerofy the marked Columns and Rows
27+ for i in range(1,len(matrix)):
28+ if(matrix[i][0]==0):
29+ for j in range(1,len(matrix[0])):
30+ matrix[i][j]=0
31+
32+ for i in range(1,len(matrix[0])):
33+ if(matrix[0][i]==0):
34+ for j in range(1,len(matrix)):
35+ matrix[j][i]=0
36+
37+ # Zerofy first Column and Row if necessary
38+ if first_Row:
39+ for i in range(0, len(matrix[0])):
40+ matrix[0][i] = 0
41+
42+ if first_Col:
43+ for i in range(0, len(matrix)):
44+ matrix[i][0] = 0
45+
46+ return matrix
You can’t perform that action at this time.
0 commit comments