Skip to content

Commit ca75dba

Browse files
authored
set matrix zeroes
1 parent c475582 commit ca75dba

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

set-matrix-zeroes/yhkee0404.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
impl Solution {
2+
pub fn set_zeroes(matrix: &mut Vec<Vec<i32>>) {
3+
let m = matrix.len();
4+
let n = matrix.first().unwrap().len();
5+
let erased_row_0 = (0..n).any(|i| matrix[0][i] == 0);
6+
let erased_col_0 = (0..m).any(|i| matrix[i][0] == 0);
7+
for i in 1..m {
8+
for j in 1..n {
9+
if matrix[i][j] == 0 {
10+
*matrix[i].first_mut().unwrap() = 0;
11+
matrix.first_mut().unwrap()[j] = 0;
12+
}
13+
}
14+
}
15+
for i in 1..m {
16+
if *matrix[i].first().unwrap() != 0 {
17+
continue;
18+
}
19+
for j in 0..n {
20+
matrix[i][j] = 0;
21+
}
22+
}
23+
for j in 1..n {
24+
if matrix.first().unwrap()[j] != 0 {
25+
continue;
26+
}
27+
for i in 0..m {
28+
matrix[i][j] = 0;
29+
}
30+
}
31+
if erased_row_0 {
32+
for j in 0..n {
33+
matrix[0][j] = 0;
34+
}
35+
}
36+
if erased_col_0 {
37+
for i in 0..m {
38+
matrix[i][0] = 0;
39+
}
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)