Dashboard
#70. Set Matrix ZeroesMediumMath & Geometry

Given an m x n matrix of integers matrix, if an element is 0, set its entire row and column to 0's.

You must update the matrix in-place.

Follow up: Could you solve it using O(1) space?

Example 1:

Input: matrix = [
  [0,1],
  [1,0]
]

Output: [
  [0,0],
  [0,0]
]

Example 2:

Input: matrix = [
  [1,2,3],
  [4,0,5],
  [6,7,8]
]

Output: [
  [1,0,3],
  [0,0,0],
  [6,0,8]
]

Constraints:

  • m == matrix.length
  • n == matrix[0].length
  • 1 <= m, n <= 200
  • -2^31 <= matrix[i][j] <= (2^31) - 1
Examples
Example 1:
Input: matrix = [ [0,1], [1,0] ]
Output: [ [0,0], [0,0] ]
Example 2:
Input: matrix = [ [1,2,3], [4,0,5], [6,7,8] ]
Output: [ [1,0,3], [0,0,0], [6,0,8] ]
Constraints
  • `m == matrix.length`
  • `n == matrix[0].length`
  • `1 <= m, n <= 200`
  • `-2^31 <= matrix[i][j] <= (2^31) - 1`
Target: O(N) time • O(1) spaceArray / Hash Table / Matrix
Socratic AI Tutor
Gemini 3.8 Flash
Loading editor...

Click "Run" or press Ctrl+Enter to execute test cases.