【一天一道LeetCode】#73. Set Matrix Zeroes

一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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的数组,如果(i,j)为0,则将第i行第j列全部元素置为0。

这道题目意思很简单,如果考虑到用辅助空间来计算就超级简单了,本题不允许用辅助空间!

那么,我们换个思路,利用矩阵自身来存储需要修改的信息。

这里我们考虑将需要修改的行列信息存储在第0行和第0列。具体见代码:

class Solution {
public:
    void setZeroes(vector<vector<int>>& matrix) {
        int row = matrix.size();
        if(row==0) return;
        int col = matrix[0].size();
        int R0flag = false;//记录第0行是否存在0
        int C0flag = false;//记录第0列是否存在0
        for(int i = 0 ; i< row ; i++)
        {
            if(matrix[i][0]==0) {C0flag=true;break;}//扫描第0列
        }
        for(int i = 0 ; i< col ; i++)
        {
            if(matrix[0][i]==0) {R0flag=true;break;}//扫描第0行
        }
        //扫描整个数组,用matrix[i][0]保存需要修改的列, matrix[0][j]保存需要修改的行
        for(int i = 0 ; i < row ;i++)
        {
            for(int j = 0 ; j < col ; j++)
            {
                if(matrix[i][j] == 0)
                {
                    matrix[i][0] = 0;
                    matrix[0][j] = 0;
                }
            }
        }
        //修改1~col-1列
        for(int i = 1 ; i< row ; i++)
        {
            if(matrix[i][0]==0){
                for(int j = 0 ; j< col ; j++)
                {
                    matrix[i][j]=0;
                }
            }
        }
        //修改1~row-1行
        for(int i = 1 ; i< col ; i++)
        {
            if(matrix[0][i]==0){
                for(int j = 0 ; j< row ; j++)
                {
                    matrix[j][i]=0;
                }
            }
        }
        //修改第0行和第0列
        if(R0flag){
            for(int i = 0 ; i < col ; i++) matrix[0][i] =0;
        }
        if(C0flag){
            for(int i = 0 ; i < row ; i++) matrix[i][0] =0;
        }
    }
};
时间: 2024-10-12 16:05:44

【一天一道LeetCode】#73. Set Matrix Zeroes的相关文章

[LeetCode#73]Set Matrix Zeroes

The problem: Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. My analysis: This is a very typical quesition in metricing our understanding about matrix. The main idea is that : We could not base on the cha

[LeetCode] 73. 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. 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

leetcode 73 Set Matrix Zeroes ----- java

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

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 使用第一行和第一列记录某

73. Set Matrix Zeroes &amp;&amp; 289. Game of Life

73. 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. Hide Tags Array Hide Similar Problems (M) Game of Life public class Solution { //Only consider the zeros that exist originally. public

【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. Could you devise a constant space solution? 思路:因为需要遍历整个矩阵,时间复杂度肯定需要O(m * n),对于空间复杂度而言,第一种是可以使用O(m * n),对每个位置的0的情况进行记录,第二种是使用O(m + n),对每行每列是否存在0进行记录,第三种是O(1)

73. Set Matrix Zeroes(js)

73. 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],   [

【LeetCode】Set Matrix Zeroes (2 solutions)

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

【LeetCode】-- 73. Set Matrix Zeroes

问题描述:将二维数组中值为0的元素,所在行或者列全set为0:https://leetcode.com/problems/set-matrix-zeroes/ 问题分析:题中要求用 constant space 的辅助空间.自然想到位优化.一个int可以存储31个元素的信息.这里刚好用到了字符串论文里面常用的优化处理方法.类似桶分的思想.好吧,这么看来这长时间的论文没白看. 附上代码: 1 void setZeroes(vector<vector<int>>& matrix