forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0073.cpp
49 lines (49 loc) · 1.27 KB
/
0073.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
#include <iostream>
#include <vector>
using namespace std;
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
class Solution
{
public:
void setZeroes(vector<vector<int>>& matrix)
{
if (matrix.empty()) return ;
bool flag = false;
unsigned int r = matrix.size(), c = matrix[0].size();
for (unsigned int i = 0; i < r; ++i)
{
if (matrix[i][0] == 0) flag = true;
for (unsigned int j = 1; j < c; ++j)
{
if (matrix[i][j] == 0)
{
matrix[0][j] = 0;
matrix[i][0] = 0;
}
}
}
for (unsigned int i = 1; i < r; ++i)
{
for (unsigned int j = 1; j < c; ++j)
{
if (!matrix[i][0] or !matrix[0][j]) matrix[i][j] = 0;
}
}
if (matrix[0][0] == 0)
{
for (unsigned int j = 0; j < c; ++j)
matrix[0][j] = 0;
}
if (flag)
{
for (unsigned int i = 0; i < r; ++i)
matrix[i][0] = 0;
}
}
};
int main()
{
vector<vector<int>> matrix = {{1,0,2,2}, {3,4,5,2}, {1,2,0,5}};
Solution().setZeroes(matrix);
return 0;
}