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+ # Set Matrix Zeroes
2+ # https://leetcode.com/problems/set-matrix-zeroes/
3+
4+ class Solution (object ):
5+ def setRowToX (self , row , matrix ):
6+ for col in range (len (matrix [0 ])):
7+ if (matrix [row ][col ] != 0 ):
8+ matrix [row ][col ] = "x"
9+
10+ def setColToX (self , col , matrix ):
11+ for row in range (len (matrix )):
12+ if (matrix [row ][col ] != 0 ):
13+ matrix [row ][col ] = "x"
14+
15+ def setToZero (self , matrix ):
16+ for row in range (len (matrix )):
17+ for col in range (len (matrix [0 ])):
18+ if (matrix [row ][col ] == "x" ):
19+ matrix [row ][col ] = 0
20+
21+ def setZeroes (self , matrix ):
22+ # set row and col of the cell that has 0 to x
23+ for row in range (len (matrix )):
24+ for col in range (len (matrix [0 ])):
25+ if (matrix [row ][col ] == 0 ):
26+ self .setRowToX (row , matrix )
27+ self .setColToX (col , matrix )
28+
29+ # set the negative numbers to zero
30+ self .setToZero (matrix )
31+
32+ """
33+ :type matrix: List[List[int]]
34+ :rtype: None Do not return anything, modify matrix in-place instead.
35+ """
36+
Original file line number Diff line number Diff line change 194194 </ul>
195195 <h2 >Matrix</h2 >
196196 <ul>
197- <li>Set Matrix Zeroes</li>
197+ <li><a href="Programs/Set Matrix Zeroes.py">Set Matrix Zeroes</a>
198+ <p><b>Approach</b>: Set the row and col of the cell with zero to value "x".
199+ Then in second pass, change all the cells with "x" to zero.
200+ This ensures we use no extra space and do it in O(n) time.
201+ </p>
202+ </li>
198203 <li>Spiral Matrix</li>
199204 <li>Rotate Image</li>
200205 <li>Word Search</li>
You can’t perform that action at this time.
0 commit comments