Skip to content

Commit a44655f

Browse files
committed
Leetcode accepted
1 parent 03402bf commit a44655f

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+

Blind 75/README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,12 @@
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>

0 commit comments

Comments
 (0)