From 5931092164cbcf17444ac73167988d0431e569f9 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Fri, 3 May 2024 12:04:47 +0800 Subject: [PATCH] 20240503 Finished --- src/problem/mod.rs | 3 +- src/problem/p73_set_matrix_zeroes.rs | 75 ++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 src/problem/p73_set_matrix_zeroes.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index 6576c5a..33d845c 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -119,4 +119,5 @@ mod p30_substring_with_concatenation_of_all_words; mod p76_minimum_window_substring; mod p36_valid_sudoku; mod p54_spiral_matrix; -mod p48_rotate_image; \ No newline at end of file +mod p48_rotate_image; +mod p73_set_matrix_zeroes; \ No newline at end of file diff --git a/src/problem/p73_set_matrix_zeroes.rs b/src/problem/p73_set_matrix_zeroes.rs new file mode 100644 index 0000000..5460a93 --- /dev/null +++ b/src/problem/p73_set_matrix_zeroes.rs @@ -0,0 +1,75 @@ +/** + * [73] Set Matrix Zeroes + */ +pub struct Solution {} + +// submission codes start here + +impl Solution { + pub fn set_zeroes(matrix: &mut Vec>) { + let m = matrix.len(); + let n = matrix[0].len(); + + let mut width_empty = false; + let mut height_empty = false; + + for i in 0..m { + if matrix[i][0] == 0 { + height_empty = true; + } + } + + for j in 0..n { + if matrix[0][j] == 0 { + width_empty = true; + } + } + + for i in 1..m { + for j in 1..n { + if matrix[i][j] == 0 { + matrix[i][0] = 0; + matrix[0][j] = 0; + } + } + } + + for i in (1..n) { + if matrix[0][i] == 0 { + for j in 0..m { + matrix[j][i] = 0; + } + } + } + + for i in 1..m { + if matrix[i][0] == 0 { + for j in 0..n { + matrix[i][j] = 0; + } + } + } + + if width_empty { + for j in 0..n { + matrix[0][j] = 0; + } + } + + if height_empty { + for i in 0..m { + matrix[i][0] = 0; + } + } + } +} + +// submission codes end + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_73() {} +}