Problem · Array

Set Matrix Zeroes

Learn this problem
MediumTekion logoTekionINTERNONSITE INTERVIEW

Problem statement

Given an m x n integer matrix matrix, if an element is 0, set every element in its row and column to 0.

Modify the matrix in place using constant extra space, then return the transformed matrix.

Function

setZeroes(matrix: int[][]) → int[][]

Examples

Example 1

matrix = [[1,1,1],[1,0,1],[1,1,1]]return = [[1,0,1],[0,0,0],[1,0,1]]

The zero at row 1, column 1 makes the entire middle row and middle column zero.

Example 2

matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]return = [[0,0,0,0],[0,4,5,0],[0,3,1,0]]

The zeroes in the first row make the first and fourth columns zero, and the first row is already required to become all zeroes.

Constraints

  • m == matrix.length.
  • n == matrix[0].length.
  • 1 <= m, n <= 200.
  • -2^31 <= matrix[i][j] <= 2^31 - 1.

More Tekion problems

drafts saved locally
public int[][] setZeroes(int[][] matrix) {
    // Write your code here.
}
matrix[[1,1,1],[1,0,1],[1,1,1]]
expected[[1,0,1],[0,0,0],[1,0,1]]
checking account