-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathset_zeroes.rs
47 lines (41 loc) · 1.21 KB
/
set_zeroes.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
// 73. Set Matrix Zeroes, Medium
// https://leetcode.com/problems/set-matrix-zeroes/submissions/
impl Solution {
pub fn set_zeroes(matrix: &mut Vec<Vec<i32>>) {
let [n, m] = [matrix.len(), matrix[0].len()];
let mut zeros: Vec<(usize, usize)> = Vec::new();
for i in 0..n {
for j in 0..m {
if matrix[i][j] == 0 {
zeros.push((i, j));
}
}
}
for (zi, zj) in zeros {
for i in 0..n {
matrix[i][zj] = 0;
}
for j in 0..m {
matrix[zi][j] = 0;
}
}
}
}
struct Solution {}
#[cfg(test)]
mod tests {
use super::*;
use crate::vec_vec_i32;
#[test]
fn test_set_zeroes() {
let mut matrix = vec_vec_i32![[1, 1, 1], [1, 0, 1], [1, 1, 1]];
Solution::set_zeroes(&mut matrix);
assert_eq!(matrix, vec_vec_i32![[1, 0, 1], [0, 0, 0], [1, 0, 1]]);
}
#[test]
fn test_set_zeroes2() {
let mut matrix = vec_vec_i32![[0, 1, 2, 0], [3, 4, 5, 2], [1, 3, 1, 5]];
Solution::set_zeroes(&mut matrix);
assert_eq!(matrix, vec_vec_i32![[0, 0, 0, 0], [0, 4, 5, 0], [0, 3, 1, 0]]);
}
}