leetcode 之Set Matrix Zeroes(10)】的更多相关文章

设置两个布尔数组,记录行和列是否存在0.需要注意的是如何将行或列设为0. void setZeros(vector<vector<int>> &matrix) { int m = matrix.size(); ].size(); vector<bool>row(m, false); vector<bool>col(n, false); ; i < m;i++) ; j < n; j++) { ) row[i] = col[j] = tru…
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 思路:不能用额外空间,就用矩阵的第一行和第一列来标记这一行或这一列是否需要置0. 用两个bool量记录第一行和第一列是否需要置0 大神的代码和我的代码都是这个思路,但是我在画0的时候是行列分开处理的,大神的代码是一起处理的 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. 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]…
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 解题思路: 用两个boolean数组row col表示行列是否有零即可,JAVA实现如下: public void setZeroes(int[][] matrix) { if (matrix.length == 0 || matrix[0].length == 0) return; boolean[] row…
原题地址 用矩形的第一行和第一列充当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++…
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. Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 分析:最直接想到的就是遍历该矩阵,每遇到0就把它所在的行和列全部置0,但这是错的,因为这样会引入新的0到矩阵中.下一个比较容易相到的方法是:遍历矩阵,每遇到一个0元素就把它所在的行和列标记起来,最后再遍历matrix,若某元素的行或者列…
今天看到CSDN博客的勋章换了图表,同一时候也添加显示了博客等级,看起来都听清新的,感觉不错! [题目] 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) sp…
给定一个矩阵,把零值所在的行和列都置为零.例如: 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(mn) space is probably a bad idea.A simple improvement uses O…