forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain3.cpp
50 lines (39 loc) · 1.26 KB
/
main3.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
47
48
49
50
/// Source : https://leetcode.com/problems/set-matrix-zeroes/description/
/// Author : liuyubobobo
/// Time : 2018-10-05
#include <iostream>
#include <vector>
using namespace std;
/// Using an sentinel value to mark zero in place
/// Attention: this method is actually wrong since we can not guarantee that
/// the sentinel value can not occur in the matrix
///
/// Time Complexity: O(m * n * (m + n))
/// Space Complexity: O(1)
class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
int m = matrix.size();
if(!m) return;
int n = matrix[0].size();
if(!n) return;
int sentinel = 2e9;
for(int i = 0; i < m; i ++)
for(int j = 0; j < n; j ++)
if(matrix[i][j] == 0){
for(int k = 0; k < n; k ++)
if(matrix[i][k] != 0)
matrix[i][k] = sentinel;
for(int k = 0; k < m; k ++)
if(matrix[k][j] != 0)
matrix[k][j] = sentinel;
}
for(int i = 0; i < m; i ++)
for(int j = 0; j < n; j ++)
if(matrix[i][j] == sentinel)
matrix[i][j] = 0;
}
};
int main() {
return 0;
}