Zero Matrix
Given an m x n matrix, if an element is 0, set its entire row and column to 0. You must do it in-place.
[ [ 1, 2, 3 ], [ 4, 0, 6 ], [ 7, 8, 9 ] ]
Explanation. The element matrix[1][1] is 0, therefore its entire row and column will be set to 0 which transforms the matrix to this output.
[ [ 0, 1 ], [ 1, 1 ] ]
Explanation. Since the element matrix[0][0] is 0, the first row and the first column are set to 0.
[ [ 2, 3, 4 ], [ 5, 6, 7 ], [ 8, 9, 0 ] ]
Explanation. The element matrix[2][2] is 0, setting its row and column to 0 results in the respective transformed matrix.
Follow-up: Can you come up with a solution that uses constant space complexity (i.e., does not depend on the size of the input matrix)?
1. The matrix dimensions will be between 1x1 and 20x20.\n2. All elements in the matrix will be integers within the range [-1000, 1000].
- Views
- 2