73. Set Matrix Zeroes? (Graph)

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?

思路:第一行和第一列用来标示该行、该列是否全0,但事先得判断第一行、第一列是否全0=>用两个额外的变量存储

class Solution {
public:
    void setZeroes(vector<vector<int> > &matrix) {
        if(matrix.empty()) return;

        bool firstLineZero = false;
        bool firstColumnZero = false;
        if(matrix[0][0]==0){
            firstLineZero = true;
            firstColumnZero = true;
        }
        //the first line
        for(int i = 1; i<matrix[0].size(); i++)
        {
            if(matrix[0][i]!=0) continue;
            firstLineZero = true;
            break;
        }
         //the first column
        for(int i = 1; i<matrix.size(); i++)
        {
            if(matrix[i][0]!=0) continue;
            firstColumnZero = true;
            break;
        }

        for(int i = 1; i < matrix.size(); i++)
        {
            for(int j = 1; j<matrix[0].size(); j++)
            {
                if(matrix[i][j] != 0) continue;
                matrix[i][0] = 0;
                matrix[0][j] = 0;
            }
        }

        for(int i = 1; i<matrix[0].size(); i++)
        {
            if(matrix[0][i]!=0) continue;
            for(int j = 1; j<matrix.size(); j++)
            {
                matrix[j][i]=0;
            }
        }
        for(int i = 1; i<matrix.size(); i++)
        {
            if(matrix[i][0]!=0) continue;
            for(int j = 1; j<matrix[0].size(); j++)
            {
                matrix[i][j]=0;
            }
        }

        if(firstLineZero)
        {
            for(int i = 0 ; i< matrix[0].size(); i++)
            {
                matrix[0][i] = 0;
            }
        }
        if(firstColumnZero)
        {
            for(int i = 0 ; i< matrix.size(); i++)
            {
                matrix[i][0] = 0;
            }
        }
    }
};
时间: 2024-10-12 04:13:55

73. Set Matrix Zeroes? (Graph)的相关文章

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

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#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

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

73. Set Matrix Zeroes java solutions

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

[leedcode 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. public class Solution { public void setZeroes(int[][] matrix) { //题目要求不使用额外空间,因此需要借助矩阵本身的空间来辅助存储, //这里借用了矩阵的第一行和第一列来辅助纪录该行或该列是否为0.由于第一行第一列自身发生了改变,再用两个变量记录第一

【一天一道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. 这道题目意思很简单,如果考虑