From f050ff6c2c1761652dfacfc31046600dad6b9e3a Mon Sep 17 00:00:00 2001 From: Suhani Kundu <126338201+suhanikundu@users.noreply.github.com> Date: Thu, 9 Oct 2025 16:12:09 +0530 Subject: [PATCH 1/2] Created set_matrix_zeroes.py --- data_structures/arrays/set_matrix_zeroes.py | 35 +++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 data_structures/arrays/set_matrix_zeroes.py diff --git a/data_structures/arrays/set_matrix_zeroes.py b/data_structures/arrays/set_matrix_zeroes.py new file mode 100644 index 000000000000..54040fa0180b --- /dev/null +++ b/data_structures/arrays/set_matrix_zeroes.py @@ -0,0 +1,35 @@ +class Solution(object): + def setZeroes(self, 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 + From 4c32c51d4bff5fae55726cfa56eb378ca2610c76 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 9 Oct 2025 10:45:30 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/set_matrix_zeroes.py | 1 - 1 file changed, 1 deletion(-) diff --git a/data_structures/arrays/set_matrix_zeroes.py b/data_structures/arrays/set_matrix_zeroes.py index 54040fa0180b..c6d6529f42e1 100644 --- a/data_structures/arrays/set_matrix_zeroes.py +++ b/data_structures/arrays/set_matrix_zeroes.py @@ -32,4 +32,3 @@ def setZeroes(self, matrix): if col0 == 0: for i in range(rows): matrix[i][0] = 0 -