码迷,mamicode.com
首页 > 编程语言 > 详细

LeetCode—Set Matrix Zeroes 矩阵数组值为0,至行,列为0

时间:2015-04-11 17:55:45      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:leetcoode   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.

题目没有什么难度,但是可以在空间复杂度上做一些处理:
开始写的算法比较简单,将行和列中为0的部分记录下来,然后再经过一个赋值操作:

class Solution {
public:
    void setZeroes(vector<vector<int> > &matrix) {
        if(matrix.empty()||matrix[0].empty())
        {
            return;
        }
        int m = matrix.size();
        int n = matrix[0].size();
        vector<int> row;
        vector<int> col;
        for(int i = 0; i < m; i++)
        {
            for(int j = 0; j < n; j++)
            {
                if(matrix[i][j] == 0)
                {
                    if(find(col.begin(),col.end(),j) == col.end())
                    {
                        col.push_back(j);
                    }
                    if(find(row.begin(),row.end(),i) == row.end())
                    {
                        row.push_back(i);
                    }
                }
            }
        }
        for(int i = 0; i < row.size(); i++)
        {
            for(int j = 0; j < n; j++)
            {
                matrix[row[i]][j] = 0;
            }
        }
        for(int i = 0; i < m; i++)
        {
            for(int j = 0; j <col.size(); j++)
            {
                 matrix[i][col[j]] = 0;
            }
        }
    }
};
还有一种比较好的做法就是利用已有的内存空间进行操作:
1 首先判断第一行,第一列是否需要置0

2 利用第一行,和第一列,记录当前位置如果为0,需要置0的行和列的位置,也就是利用第一行,第一列保存中间值

3 对应行,列置0

4 第一行,第一列进行处理:

void setZeroes(vector<vector<int> > &matrix) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        int row = matrix.size();
        if(row == 0) return;
        int col = matrix[0].size();
        if(col == 0) return;
        
        bool firstrowiszero = false;
        bool firstcoliszero = false;
        for(int j = 0; j < col; ++j)
            if(matrix[0][j] == 0){
                firstrowiszero = true;
                break;
            }
        for(int i = 0; i < row; ++i)
            if(matrix[i][0] == 0){
                firstcoliszero = true;
                break;
            }
        
        for(int i = 1; i < row; ++i)
            for(int j = 1; j < col; ++j){
                if(matrix[i][j] == 0) {
                    matrix[i][0] = 0;
                    matrix[0][j] = 0;
                }
            }
            
        for(int i = 1; i < row; ++i)
            for(int j = 1; j < col; ++j)
                if(matrix[i][0] == 0 || matrix[0][j] == 0)
                    matrix[i][j] = 0;
        
        if(firstrowiszero){
            for(int j = 0; j < col; ++j)
                matrix[0][j] = 0;
        }
        if(firstcoliszero){
            for(int i = 0; i < row; ++i)
                matrix[i][0] = 0;
        }
    }



LeetCode—Set Matrix Zeroes 矩阵数组值为0,至行,列为0

标签:leetcoode   set matrix zeroes   内存空间   

原文地址:http://blog.csdn.net/xietingcandice/article/details/44997173

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