Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

18. Set Matrix Zeroes

MediumDay 5 of 60Arrays & Hashing

Given an m x n integer matrix, whenever a cell is 0, set its entire row and its entire column to 0. Do it in place. Only zeros present in the original matrix count, not zeros you write.

Example 1
Input: matrix = [[1,1,1],[1,0,1],[1,1,1]]
Output: [[1,0,1],[0,0,0],[1,0,1]]
The zero at row 1, column 1 clears row 1 and column 1.
Example 2
Input: matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
Output: [[0,0,0,0],[0,4,5,0],[0,3,1,0]]
Constraints
  • 1 <= m, n <= 200
  • -2^31 <= matrix[i][j] <= 2^31 - 1
Hints
Frequently asked at: Microsoft, Amazon, Apple, Oracle, Samsung
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min