diff options
author | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
commit | 7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch) | |
tree | eb7a24ee2067f92245c57bb118e85a1baf15df50 /other/set_matrix_0.rs | |
parent | 5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff) | |
download | leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip |
chore: move cpp and rust code to directory
Diffstat (limited to 'other/set_matrix_0.rs')
-rw-r--r-- | other/set_matrix_0.rs | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/other/set_matrix_0.rs b/other/set_matrix_0.rs new file mode 100644 index 0000000..78c434a --- /dev/null +++ b/other/set_matrix_0.rs @@ -0,0 +1,44 @@ +// 73. Set Matrix Zeroes +// given an 'm x n' integer matrix 'matrix', if an element is 0, +// set its entire row and column to 0's. must be done in place +// (https://en.wikipedia.org/wiki/In-place_algorithm) + +impl Solution { + pub fn set_zeroes(matrix: &mut Vec<Vec<i32>>) { + let n_rows = matrix.len(); + let n_cols = matrix[0].len(); + let mut first_row = false; + let mut first_col = false; + for row in 0..n_rows { + for col in 0..n_cols { + if matrix[row][col] == 0 { + if row == 0 { + first_row = true; + } + if col == 0 { + first_col = true; + } + matrix[0][col] = 0; + matrix[row][0] = 0; + } + } + } + for row in 1..n_rows { + for col in 1..n_cols { + if matrix[row][0] == 0 || matrix[0][col] == 0 { + matrix[row][col] = 0 + } + } + } + if first_row { + for col in 0..n_cols { + matrix[0][col] = 0; + } + } + if first_col { + for row in 0..n_rows { + matrix[row][0] = 0; + } + } + } +} |