FastPrepSet Matrix Zeroes

Set Matrix Zeroes

Pinecone logoPinecone● MediumFULLTIMEOA
Learn

Problem statement

Given an m x n integer matrix matrix, if any cell is 0, set every cell in that row and column to 0.

Modify the input matrix and 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 middle row and column contain the original zero, so both become 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 original zeroes mark the first row and the first and fourth columns.

Constraints

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

More Pinecone problems

See Pinecone hiring insights
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…