forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain2.cpp
46 lines (34 loc) · 988 Bytes
/
main2.cpp
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/// Source : https://leetcode.com/problems/set-matrix-zeroes/description/
/// Author : liuyubobobo
/// Time : 2018-10-05
#include <iostream>
#include <vector>
using namespace std;
/// Only record the zero row index and col index
/// Time Complexity: O(m * n)
/// Space Complexity: O(m + n)
class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
int m = matrix.size();
if(!m) return;
int n = matrix[0].size();
if(!n) return;
vector<int> zeroRows, zeroCols;
for(int i = 0; i < m; i ++)
for(int j = 0; j < n; j ++)
if(!matrix[i][j]){
zeroRows.push_back(i);
zeroCols.push_back(j);
}
for(int r: zeroRows)
for(int j = 0; j < n; j ++)
matrix[r][j] = 0;
for(int c: zeroCols)
for(int i = 0; i < m; i ++)
matrix[i][c] = 0;
}
};
int main() {
return 0;
}