Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place. Example 1: Input: [   [1,1,1],   [1,0,1],   [1,1,1] ] Output: [   [1,0,1],   [0,0,0],   [1,0,1] ] Example 2: Input: [   [0,1,2,0],   [3,4,5,2],   [1,3,1,5]…
1.7 Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are set to 0. LeetCode中的原题,请参见我之前的博客Set Matrix Zeroes 矩阵赋零.…
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click to show follow up. 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…
给定一个矩阵,把零值所在的行和列都置为零.例如: 1 2 3 1 3 1 1 1 操作之后变为 1 3 0 0 0 1 1 方法1: 赋值另存一个m*n的矩阵,在原矩阵为零的值相应置新的矩阵行和列为零.额外空间为O(m*n). 方法2: 两个数组,bool[m] 和 bool[n] 分别存某行有零,后者某列有零.之后根据数组值将原矩阵相应位置置零.额外空间O(m + n). class Solution { public: void setZeroes(vector<vector<int>…
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click to show follow up. Follow up: Did you use extra space?A straight forward solution using O(m n) space is probably a bad idea.A simple improvement uses…
给定一个 m x n 的矩阵,如果一个元素为 0 ,则将这个元素所在的行和列都置零.你有没有使用额外的空间?使用 O(mn) 的空间不是一个好的解决方案.使用 O(m + n) 的空间有所改善,但仍不是最好的解决方案.你能设计一个使用恒定空间的解决方案吗?详见:https://leetcode.com/problems/set-matrix-zeroes/description/ Java实现: class Solution { public void setZeroes(int[][] mat…
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…
给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0.请使用原地算法. 示例 1: 输入: [   [1,1,1],   [1,0,1],   [1,1,1] ] 输出: [   [1,0,1],   [0,0,0],   [1,0,1] ] 示例 2: 输入: [   [0,1,2,0],   [3,4,5,2],   [1,3,1,5] ] 输出: [   [0,0,0,0],   [0,4,5,0],   [0,3,1,0] ] 进阶: 一个直接的解决方案…
原题地址 用矩形的第一行和第一列充当mask 代码: void setZeroes(vector<vector<int> > &matrix) { ].empty()) return; bool firstRow = false; bool firstCol = false; int m = matrix.size(); ].size(); ; i < m; i++) ] == ) { firstCol = true; break; } ; j < n; j++…
Set Matrix Zeroes Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click to show follow up. Follow up: Did you use extra space?A straight forward solution using O(mn) space is probably a bad idea.A simple…