forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution2.rs
51 lines (51 loc) · 1.19 KB
/
Solution2.rs
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
51
impl Solution {
pub fn set_zeroes(matrix: &mut Vec<Vec<i32>>) {
let m = matrix.len();
let n = matrix[0].len();
let l0 = {
let mut res = false;
for j in 0..n {
if matrix[0][j] == 0 {
res = true;
break;
}
}
res
};
let r0 = {
let mut res = false;
for i in 0..m {
if matrix[i][0] == 0 {
res = true;
break;
}
}
res
};
for i in 0..m {
for j in 0..n {
if matrix[i][j] == 0 {
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}
for i in 1..m {
for j in 1..n {
if matrix[i][0] == 0 || matrix[0][j] == 0 {
matrix[i][j] = 0;
}
}
}
if l0 {
for j in 0..n {
matrix[0][j] = 0;
}
}
if r0 {
for i in 0..m {
matrix[i][0] = 0;
}
}
}
}