Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
Follow up:
Did you use extra space?
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?
描述
给出一个 m * n
的整型数组,如果数组元素为 0
,则将其所在的行和列都置为 0
分析
将数组内 0
所在的行和列储存于第一行和第一列中,最后根据第一行和第一列进行更新。此外,需要使用两个变量来存储第一行和第一列原本的情况,避免被后面的数据影响
代码
1 | public class Solution { |