leetcode 73. Set Matrix Zeroes

来源:互联网 发布:rt809f编程器软件下载 编辑:程序博客网 时间:2024/06/08 07:20

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


class Solution {public:void setZeroes(vector<vector<int>>&matrix){if (matrix.empty()||matrix[0].empty())return;set<int>rows, cols;for (int i = 0; i < matrix.size(); i++){for (int j = 0; j < matrix[0].size(); j++){if (matrix[i][j] == 0){rows.insert(i);cols.insert(j);}}}for (set<int>::iterator it = rows.begin(); it != rows.end(); it++)for (int j = 0; j < matrix[0].size(); j++)matrix[*it][j] = 0;for (set<int>::iterator it = cols.begin(); it != cols.end(); it++)for (int j = 0; j < matrix.size(); j++)matrix[j][*it] = 0;}};

accepted

0 0
原创粉丝点击