Set Matrix Zeroes
Detailed guide and Python implementation for the 'Set Matrix Zeroes' problem.
Problem Statement
Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's.
You must do it in place.
Implement a function setZeroes(matrix: list) -> list that modifies the matrix in place and returns it.
- •m == matrix.length
- •n == matrix[0].length
- •1 <= m, n <= 200
- •-2^31 <= matrix[i][j] <= 2^31 - 1
Examples
[[1,1,1],[1,0,1],[1,1,1]]
[[1,0,1],[0,0,0],[1,0,1]]
The element at position (1,1) is 0. So the entire row 1 and column 1 are set to 0.
[[0,1,2,0],[3,4,5,2],[1,3,1,5]]
[[0,0,0,0],[0,4,5,0],[0,3,1,0]]
Elements at (0,0) and (0,3) are 0. Row 0 becomes all zeros. Columns 0 and 3 become all zeros.
Need a Hint?
Edge Cases to Watch
- Empty input structures
- Single element inputs
- Large numerical bounds
Ready to Solve?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.
Recommended Python Resources
Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.
Python Sets
Master sets in Python. Learn to store unique values, execute intersections, unions, differences, and understand the performance benefits of hashing.
How to Sort a List in Python
Learn how to sort a list in Python using the sort() method and the sorted() function. Discover custom key sorting and reverse order examples.
Python Set Methods
Complete guide to Python set operations. Learn how to add, remove, and perform mathematical set operations like unions and intersections.
Python vs JavaScript: Which Programming Language is Best?
A comprehensive comparison between Python and JavaScript. Explore syntax differences, performance, use cases (backend vs frontend), and coding examples.