leetcode_set_matrix_zeroes

难度:Medium

解题思路:找出应该设置为0的行和列,然后操作。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
int m = matrix.size();
int n = matrix[0].size();
set<int> rows;
set<int> cols;
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
if(matrix[i][j] == 0)
{
rows.insert(i);
cols.insert(j);
}
}
}
auto rows_it = rows.begin();
for(; rows_it != rows.end(); rows_it++)
{
for( int j = 0; j < n; j++)
matrix[*rows_it][j] = 0;
}
auto cols_it = cols.begin();
for(; cols_it != cols.end(); cols_it++)
{
for(int i = 0; i < m; i++)
matrix[i][*cols_it] = 0;
}
}
};

运行结果:66ms,超过20.51%