Description

Given an m x n integer matrix, if an element is 0, set its entire row and column to 0's. You must do it in place.

Examples

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

Found 0 at position (1,1). First pass: mark row 1 and column 1 for zeroing. Second pass: set all elements in row 1 to 0, giving [0,0,0]. Set all elements in column 1 to 0, changing column 1 of rows 0 and 2. Final matrix: [[1,0,1],[0,0,0],[1,0,1]].

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]]
Explanation:

Found zeros at positions (0,0) and (0,3). Row 0 contains two zeros, so entire row becomes [0,0,0,0]. Column 0 gets zeroed: row 1 col 0 becomes 0, row 2 col 0 becomes 0. Column 3 gets zeroed: row 1 col 3 becomes 0, row 2 col 3 becomes 0. Result: [[0,0,0,0],[0,4,5,0],[0,3,1,0]].

Input:matrix = [[5,0],[8,9],[2,7]]
Output:[[0,0],[8,0],[2,0]]
Explanation:

Found 0 at position (0,1) in the 3x2 matrix. Row 0 becomes [0,0] (both elements zeroed). Column 1 becomes [0,0,0] (all three rows affected). Original values 5, 9, 7 in column 1 are replaced with zeros, while 8 and 2 in column 0 remain unchanged.

Constraints

  • m == matrix.length
  • n == matrix[0].length
  • 1 ≤ m, n ≤ 200
  • -2³¹ ≤ matrix[i][j] ≤ 2³¹ - 1

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!