码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode_73_Set Matrix Zeroes

时间:2015-06-22 11:12:40      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:array   c++   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.

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?


分析:(详细见代码注释)
方法一:空间复杂度 O(m+n),很简单,设置两个 bool 数组,记录每行和每列是否存在 0。
方法二:空间复杂度 O(1),复用第一行和第一列的数组。



//方法一:O ( m + n ) 空间的方法很简单,设置两个 bool 数组,记录每行和每列是否存在 0。
//时间复杂度 O(m*n) ,空间复杂度 O(m+n)
class Solution {
public:
    void setZeroes(vector<vector<int>>& matrix) {
        int m = matrix.size();
        int n = matrix[0].size();
        if(m == 0 || n == 0)
            return ;
        vector<bool> row(m, false);
        vector<bool> column(n, false);
        
        for(int i = 0; i < m; i++) 
            for(int j = 0; j < n; j++)
            {
                if(!matrix[i][j])
                {
                    row[i] = true;
                    column[j] = true;
                }
            }
        
        for(int i = 0; i < m; i++)
        {
            if(row[i])
                fill(&matrix[i][0], &matrix[i][0] + n, 0); //&matrix[i][0]
        }
        
        for(int j = 0; j < n; j++)
        {
            if(column[j])
            {
                for(int i = 0; i < m; i++)
                    matrix[i][j] = 0;
            }
        }
    }
};

//方法一(另一种写法):O ( m + n ) 空间的方法很简单,设置两个 bool 数组,记录每行和每列是否存在 0。
//时间复杂度 O(m*n) ,空间复杂度 O(m+n)
class Solution {
public:
    void setZeroes(vector<vector<int>>& matrix) {
        int m = matrix.size();
        int n = matrix[0].size();
        if(m == 0 || n == 0)
            return ;
        vector<bool> row(m, false);
        vector<bool> column(n, false);
        
        for(int i = 0; i < m; i++) 
            for(int j = 0; j < n; j++)
            {
                if(!matrix[i][j])
                {
                    row[i] = true;
                    column[j] = true;
                }
            }
        
        for(int i = 0; i < m; i++)
            for(int j = 0; j < n; j++)
            {
                if(row[i] || column[j])
                    matrix[i][j] = 0;
            }
    }
};

//方法二:复用第一行和第一列的数组。
//时间复杂度 O(m*n) ,空间复杂度 O(1)
class Solution {
public:
    void setZeroes(vector<vector<int>>& matrix) {
        int m = matrix.size();
        int n = matrix[0].size();
        if(m == 0 || n == 0)
            return ;
        bool row = false; //第一行是否有0
        bool column = false; //第一列是否有0
        
        //判断第一行和第一列是否有0,然后复用这两个数组
        for(int i = 0; i < m; i++)
        {
            if(!matrix[i][0])
            {
                column = true;
                break;
            }
        }
        for(int j = 0; j < n; j++)
        {
            if(!matrix[0][j])
            {
                row = true;
                break;
            } 
        }
        
        //判断第一行和第一列以外的,是否有0,有则根据复用的数组情况赋0
        for(int i = 1; i < m; i++)
            for(int j = 1; j < n; j++)
            {
                if(!matrix[i][j])
                {
                    matrix[i][0] = 0; //因为是赋值为0,所以这两个数组不需要初始化
                    matrix[0][j] = 0;
                }
            }
        for(int i =1 ; i < m; i++)
            for(int j = 1; j < n; j++)
            {
                if(matrix[i][0] == 0 || matrix[0][j] == 0)
                    matrix[i][j] = 0;
            }
        
        //最后,将第一行和第一列判断并赋值
        if(row)
        {
            for(int j = 0; j < n; j++)
                matrix[0][j] = 0;
        }
        if(column)
        {
            for(int i = 0; i < m; i++)
                matrix[i][0] = 0;
        }
    }
};









leetcode_73_Set Matrix Zeroes

标签:array   c++   leetcode   

原文地址:http://blog.csdn.net/keyyuanxin/article/details/46591669

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!