leetcode---set-matrix-zeroes---查找

来源:互联网 发布:矢量图软件 手机版 编辑:程序博客网 时间:2024/06/05 00:11

题目描述
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 O(m + n) space, but still not the best solution.
Could you devise a constant space solution?

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