-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset_matrix_zeroes.rs
101 lines (94 loc) · 1.99 KB
/
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
///
/// Problem: Set Matrix Zeroes
///
/// Given an `m x n` integer matrix, if an element is `0`, set its **entire row and column** to `0`.
///
/// You must do it **in place** with `O(1)` extra space.
///
/// **Example 1:**
/// ```plaintext
/// Input: matrix =
/// [
/// [1,1,1],
/// [1,0,1],
/// [1,1,1]
/// ]
///
/// Output:
/// [
/// [1,0,1],
/// [0,0,0],
/// [1,0,1]
/// ]
/// ```
///
/// **Example 2:**
/// ```plaintext
/// Input: matrix =
/// [
/// [0,1,2,0],
/// [3,4,5,2],
/// [1,3,1,5]
/// ]
///
/// Output:
/// [
/// [0,0,0,0],
/// [0,4,5,0],
/// [0,3,1,0]
/// ]
/// ```
///
/// **Constraints:**
/// - `m == matrix.length`
/// - `n == matrix[i].length`
/// - `1 <= m, n <= 200`
/// - `-2^31 <= matrix[i][j] <= 2^31 - 1`
///
/// # Solution:
/// - **Time Complexity:** `O(m * n)`
/// - **Space Complexity:** `O(1)`
impl Solution {
pub fn set_zeroes(matrix: &mut Vec<Vec<i32>>) {
let (m, n) = (matrix.len(), matrix[0].len());
let mut first_row_has_zero = false;
let mut first_col_has_zero = false;
for i in 0..m {
if matrix[i][0] == 0 {
first_col_has_zero = true;
break;
}
}
for j in 0..n {
if matrix[0][j] == 0 {
first_row_has_zero = true;
break;
}
}
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..m {
for j in 1..n {
if matrix[i][0] == 0 || matrix[0][j] == 0 {
matrix[i][j] = 0;
}
}
}
if first_col_has_zero {
for i in 0..m {
matrix[i][0] = 0;
}
}
if first_row_has_zero {
for j in 0..n {
matrix[0][j] = 0;
}
}
}
}