Zero Matrix
Given a matrix of integers, if an element in the matrix is 0, set its entire row and column to 0. Return the modified matrix.
Examples:
-
Input:
[[1, 2, 3], [4, 0, 6], [7, 8, 9]]Output:[[1, 0, 3], [0, 0, 0], [7, 0, 9]] -
Input:
[[0, 2], [3, 4]]Output:[[0, 0], [0, 4]]
[ [ 1, 2, 3 ], [ 4, 0, 6 ], [ 7, 8, 9 ] ]
Explanation. Zero is found at matrix[1][1], set all items in row 1 and column 1 to zero.
[ [ 0, 2 ], [ 3, 4 ] ]
Explanation. Zero is found at matrix[0][0], set all items in row 0 and column 0 to zero.
[ [ 7, 8, 9 ], [ 11, 0, 5 ], [ 16, 17, 18 ] ]
Explanation. Zero is found at matrix[1][1], set all items in row 1 and column 1 to zero.
Follow-up: What would be an efficient way to solve this problem if you could not use extra space for another matrix?
1. The number of rows and columns of the matrix will be at least 1 and at most 100. 2. Each integer in the matrix is between -1000 and 1000.
- Views
- 3