Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions data_structures/arrays/set_matrix_zeroes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Solution(object):

Check failure on line 1 in data_structures/arrays/set_matrix_zeroes.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (UP004)

data_structures/arrays/set_matrix_zeroes.py:1:16: UP004 Class `Solution` inherits from `object`
def setZeroes(self, matrix):

Check failure on line 2 in data_structures/arrays/set_matrix_zeroes.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N802)

data_structures/arrays/set_matrix_zeroes.py:2:9: N802 Function name `setZeroes` should be lowercase

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file data_structures/arrays/set_matrix_zeroes.py, please provide doctest for the function setZeroes

Please provide return type hint for the function: setZeroes. If the function does not return a value, please provide the type hint as: def function() -> None:

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: setZeroes

Please provide type hint for the parameter: matrix

"""
:type matrix: List[List[int]]
:rtype: None Do not return anything, modify matrix in-place instead.
"""
rows = len(matrix)
cols = len(matrix[0])
col0 = 1

# Step 1: Use first row and column as markers
for i in range(rows):
if matrix[i][0] == 0:
col0 = 0
for j in range(1, cols):
if matrix[i][j] == 0:
matrix[i][0] = 0
matrix[0][j] = 0

# Step 2: Update cells based on markers
for i in range(1, rows):
for j in range(1, cols):
if matrix[i][0] == 0 or matrix[0][j] == 0:
matrix[i][j] = 0

# Step 3: Handle first row
if matrix[0][0] == 0:
for j in range(cols):
matrix[0][j] = 0

# Step 4: Handle first column
if col0 == 0:
for i in range(rows):
matrix[i][0] = 0
Loading