Leecode 0073. Set Matrix Zeroes
73. Set Matrix Zeroes
题目
Given an *m* x *n*
matrix. If an element is 0, set its entire row and column to 0. Do it in-place.
Follow up:
- A straight forward solution using O(mn) space is probably a bad idea.
- A simple improvement uses O(m + n) space, but still not the best solution.
- Could you devise a constant space solution?
Example 1:
1 | Input: matrix = [[1,1,1],[1,0,1],[1,1,1]] |
Example 2:
1 | Input: matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]] |
Constraints:
m == matrix.length
n == matrix[0].length
1 <= m, n <= 200
2^31 <= matrix[i][j] <= 2^31 - 1
题目大意
给定一个 m x n
的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0。请使用原地算法。
解题思路
- 此题考查对程序的控制能力,无算法思想。题目要求采用原地的算法,所有修改即在原二维数组上进行。在二维数组中有 2 个特殊位置,一个是第一行,一个是第一列。它们的特殊性在于,它们之间只要有一个 0,它们都会变为全 0 。先用 2 个变量记录这一行和这一列中是否有 0,防止之后的修改覆盖了这 2 个地方。然后除去这一行和这一列以外的部分判断是否有 0,如果有 0,将它们所在的行第一个元素标记为 0,所在列的第一个元素标记为 0 。最后通过标记,将对应的行列置 0 即可。
代码
1 | #include <vector> |
All articles on this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated.