LeetCode (Set Matrix Zeroes)

来源:互联网 发布:递归算法反汇编 编辑:程序博客网 时间:2024/06/17 23:15

Problem:

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.

Solution:

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


0 0