Set Matrix Zeroes 将矩阵置零

给定一个矩阵,把零值所在的行和列都置为零。例如:

1 2 3

1 0 3

1 1 1

操作之后变为

1 0 3

0 0 0

1 0 1

方法1:

赋值另存一个m*n的矩阵,在原矩阵为零的值相应置新的矩阵行和列为零。额外空间为O(m*n).

方法2:

两个数组,bool[m] 和 bool[n] 分别存某行有零,后者某列有零。之后根据数组值将原矩阵相应位置置零。额外空间O(m + n)。

class Solution {
public:
    void setZeroes(vector<vector<int> > &matrix) {
        int len1 = matrix.size();
        if (len1 == 0) return ;
        int len2 = matrix[0].size();
        if (len2 == 0) return ;
        vector<bool > row(len1), col(len2);
        for (int i = 0; i < len1; i++)
            for (int j = 0; j < len2; j++)
            {
                if (matrix[i][j] == 0)
                {
                    row[i] = true; col[j] = true;
                }
            }
        for (int i = 0; i < len1; i++)
            for (int j = 0; j < len2; j++)
            {
                if (row[i] == true)
                    matrix[i][j] = 0;
                else if (col[j] == true)
                    matrix[i][j] = 0;
            }
        return ;
    }
};

方法3:(常数额外空间)

1. 找到一个零的位置,把这行这列当做方法2中的两个数组存值。

2. 根据1的位置的所在行和列的值是否有零将矩阵相应位置置零。

3. 再把1中零所在位置的行和列置零。

class Solution {
public:
    void setZeroes(vector<vector<int> > &matrix) {
        int len1 = matrix.size();
        if (len1 == 0) return ;
        int len2 = matrix[0].size();
        if (len2 == 0) return ;
        int row = -1, col = -1;
        for (int i = 0; i < len1; i++)
            for (int j = 0; j < len2; j++)
            {
                if (matrix[i][j] == 0)
                {
                    row = i; col = j;
                }
            }
        if (row == -1) return;
        for (int i = 0; i < len1; i++)
            for (int j = 0; j < len2; j++)
            {
                if (matrix[i][j] == 0 && i != row && j != col)
                {
                    matrix[i][col] = 0;
                    matrix[row][j] = 0;
                }
            }
        for (int i = 0; i < len1; i++)
            for (int j = 0; j < len2; j++)
            {
                if (i != row && j != col)
                {
                    if (matrix[i][col] == 0 )
                        matrix[i][j] = 0;
                    else if (matrix[row][j] == 0)
                        matrix[i][j] = 0;
                }
            }
        int index = -1;
        while(++index < len1) matrix[index][col] = 0;
        index = -1;
        while(++index < len2) matrix[row][index] = 0;
        return ;
    }
};
时间: 2024-08-02 09:44:35

Set Matrix Zeroes 将矩阵置零的相关文章

【LeetCode-面试算法经典-Java实现】【070-Set Matrix Zeroes(矩阵置零)】

[070-Set Matrix Zeroes(矩阵置零)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 题目大意 给定一个m*n的矩阵,如果某个位置是0.将对应的行和列设置为0. 解题思路 先对矩阵进行扫描,标记要进行置0的行和列,对要进行置0的行在第0列上进行标记,对置0的列在

LeetCode第[73]题(Java):Set Matrix Zeroes(矩阵置0)

题目:矩阵置0 难度:Easy 题目内容: Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place. 翻译: 给定一个m x n矩阵,如果一个元素是0,就把它的整行和列设为0. 要求:就地置0. Example 1: Input: [   [1,1,1],   [1,0,1],   [1,1,1] ] Output: [   [1,0,1],   [0,0,0],  

代码题(38)— 旋转图像、矩阵置零

1.48. 旋转图像 给定一个 n × n 的二维矩阵表示一个图像. 将图像顺时针旋转 90 度. 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵.请不要使用另一个矩阵来旋转图像. 示例 1: 给定 matrix = [ [1,2,3], [4,5,6], [7,8,9] ], 原地旋转输入矩阵,使其变为: [ [7,4,1], [8,5,2], [9,6,3] ] 示例 2: 给定 matrix = [ [ 5, 1, 9,11], [ 2, 4, 8,10], [13,

LeetCode:Set Matrix Zeroes - 将矩阵内含有零的行和列全部置零

1.题目名称 Set Matrix Zeroes(将矩阵内含有零的行和列全部置零) 2.题目地址 https://leetcode.com/problems/set-matrix-zeroes/ 3.题目内容 英文:Given a m x n matrix, if an element is 0, set its entire row and column to 0. 中文:给定一个m×n的矩阵,如果其中一个元素是0,则将该元素所在的整行或整理全部置为0 4.解题方法1 使用第一行和第一列记录某

(每日算法)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. 最直观的解法就是开辟一个新的矩阵,当原矩阵存在零元素的时候,就将新矩阵的对应行和列置为零.这样空间复杂度较高,也是题目不允许的. 题目的难点就在于,如果遇到零元素之后马上在矩阵上操作,将所在的行和列都置为零.在接下来的遍历中,如果你再遇到零,你讲不

73#矩阵置零

题目描述 给定一个 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] ] 进阶: 一个直接的解决方案是使用 O(*m**n*) 的额外空间

矩阵置零

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place. 来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/set-matrix-zeroes著作权归领扣网络所有.商业转载请联系官方授权,非商业转载请注明出处. 要求把0所在的行和列的其他元素全部置零,可以先遍历矩阵,找到所有元素为0的位置并记录,再根据遍历的结果把其

[Swift]LeetCode73. 矩阵置零 | 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. 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]

[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(m n) space is probably a bad idea.A simple improvement uses