-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy path0073-set-matrix-zeroes.rs
124 lines (106 loc) · 3.77 KB
/
0073-set-matrix-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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
Problem: LeetCode 73 - Set Matrix Zeroes
Key Idea:
The key idea is to use the first row and the first column to store information about whether the corresponding row or column should be zeroed.
Approach:
1. Initialize two boolean variables, 'first_row_zero' and 'first_col_zero', to keep track of whether the first row and the first column should be zeroed.
2. Iterate through the matrix starting from the second row and the second column.
3. If a cell in the matrix is zero, set the corresponding cell in the first row and the first column to zero to mark that the row or column should be zeroed.
4. Iterate through the matrix again and set the cells to zero based on the information in the first row and the first column.
5. If 'first_row_zero' is true, zero out the entire first row.
6. If 'first_col_zero' is true, zero out the entire first column.
7. Return the modified matrix.
Time Complexity:
O(m * n), where 'm' is the number of rows and 'n' is the number of columns in the matrix. We visit each cell in the matrix twice.
Space Complexity:
O(1), as we use the first row and the first column of the matrix to store information and do not use any additional data structures.
*/
impl Solution {
pub fn set_zeroes(matrix: &mut Vec<Vec<i32>>) {
let mut first_row_zero = false;
let mut first_col_zero = false;
let m = matrix.len();
let n = matrix[0].len();
// Check if the first row should be zeroed.
for i in 0..m {
if matrix[i][0] == 0 {
first_col_zero = true;
break;
}
}
// Check if the first column should be zeroed.
for j in 0..n {
if matrix[0][j] == 0 {
first_row_zero = true;
break;
}
}
// Mark rows and columns to be zeroed using the first row and first column.
for i in 1..m {
for j in 1..n {
if matrix[i][j] == 0 {
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}
// Zero out rows based on the information in the first column.
for i in 1..m {
if matrix[i][0] == 0 {
for j in 1..n {
matrix[i][j] = 0;
}
}
}
// Zero out columns based on the information in the first row.
for j in 1..n {
if matrix[0][j] == 0 {
for i in 1..m {
matrix[i][j] = 0;
}
}
}
// Zero out the first row if necessary.
if first_row_zero {
for j in 0..n {
matrix[0][j] = 0;
}
}
// Zero out the first column if necessary.
if first_col_zero {
for i in 0..m {
matrix[i][0] = 0;
}
}
}
}
/*
// Using HashSet
use std::collections::HashSet;
impl Solution {
pub fn set_zeroes(matrix: &mut Vec<Vec<i32>>) {
let (rows, cols) = (matrix.len(), matrix[0].len());
let mut rows_with_zero = HashSet::new();
let mut cols_with_zero = HashSet::new();
// Find rows and columns with zeros
for (r, row) in matrix.iter().enumerate() {
for (c, &val) in row.iter().enumerate() {
if val == 0 {
rows_with_zero.insert(r);
cols_with_zero.insert(c);
}
}
}
// Set rows with zeros to all zeros
for &r in &rows_with_zero {
matrix[r].iter_mut().map(|val| *val = 0).count();
}
// Set columns with zeros to all zeros
for &c in &cols_with_zero {
for row in matrix.iter_mut() {
row[c] = 0;
}
}
}
}
*/