Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1ba26d0

Browse files
committed
solve set_matrix_zeroes
1 parent cc7ce3d commit 1ba26d0

File tree

2 files changed

+163
-1
lines changed

2 files changed

+163
-1
lines changed

‎algs/0073.set_matrix_zeros.go‎

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package algs
2+
3+
// SetZeroes
4+
//
5+
// Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's.
6+
//
7+
// You must do it in place.
8+
//
9+
// Example 1:
10+
//
11+
// Input: matrix = [[1,1,1],[1,0,1],[1,1,1]]
12+
// Output: [[1,0,1],[0,0,0],[1,0,1]]
13+
//
14+
// Example 2:
15+
//
16+
// Input: matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
17+
// Output: [[0,0,0,0],[0,4,5,0],[0,3,1,0]]
18+
//
19+
// Constraints:
20+
//
21+
// m == matrix.length
22+
// n == matrix[0].length
23+
// 1 <= m, n <= 200
24+
// -231 <= matrix[i][j] <= 231 - 1
25+
//
26+
// Follow up:
27+
//
28+
// A straightforward solution using O(mn) space is probably a bad idea.
29+
// A simple improvement uses O(m + n) space, but still not the best solution.
30+
// Could you devise a constant space solution?
31+
func SetZeroes(matrix [][]int) {
32+
// // 直观解法
33+
// m := len(matrix)
34+
// n := len(matrix[0])
35+
// zeros := [2]map[int]bool {
36+
// 0: make(map[int]bool),
37+
// 1: make(map[int]bool),
38+
// }
39+
// for i := 0; i < m; i++ {
40+
// for j := 0; j < n; j++ {
41+
// if matrix[i][j] == 0 {
42+
// zeros[0][i] = true
43+
// zeros[1][j] = true
44+
// }
45+
// }
46+
// }
47+
48+
// for i := 0; i < m; i++ {
49+
// for j := 0; j < n; j++ {
50+
// if zeros[0][i] || zeros[1][j] {
51+
// matrix[i][j] = 0
52+
// }
53+
// }
54+
// }
55+
56+
// 空间复杂度为 O(1) 的解法
57+
// 使用 matrix 的初行初列代替上述 map 存储待置零行列标记
58+
//
59+
//
60+
// 使用初行初列标记那些行哪些列应该等于0
61+
var rowZero, columZero bool
62+
for i := 0; i < len(matrix); i++ {
63+
for j := 0; j < len(matrix[i]); j++ {
64+
if matrix[i][j] == 0 {
65+
matrix[i][0] = 0
66+
matrix[0][j] = 0
67+
if i == 0 {
68+
// 标记第一行中是否有 0
69+
rowZero = true
70+
}
71+
if j == 0 {
72+
// 标记第一列中是否有0
73+
columZero = true
74+
}
75+
}
76+
}
77+
}
78+
79+
for i := 1; i < len(matrix); i++ {
80+
for j := 1; j < len(matrix[i]); j++ {
81+
if matrix[i][0] == 0 || matrix[0][j] == 0 {
82+
matrix[i][j] = 0
83+
}
84+
}
85+
}
86+
if rowZero {
87+
for i := 0; i < len(matrix[0]); i++ {
88+
matrix[0][i] = 0
89+
}
90+
}
91+
if columZero {
92+
for i := 0; i < len(matrix); i++ {
93+
matrix[i][0] = 0
94+
}
95+
}
96+
}

‎src/solution.rs‎

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,7 @@ impl Solution {
874874
result
875875
}
876876

877-
/// [41. Frist Missing Positive]
877+
/// [41. Frist Missing Positive](https://leetcode.cn/problems/first-missing-positive)
878878
///
879879
/// Given an unsorted integer array nums. Return the smallest positive integer that is not present in nums.
880880
///
@@ -932,4 +932,70 @@ impl Solution {
932932
}
933933
nums.len() as i32 + 1
934934
}
935+
936+
/// [73. Set Matrix Zeros](https://leetcode.cn/problems/set-matrix-zeroes/description)
937+
///
938+
/// Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's.
939+
///
940+
/// You must do it in place.
941+
///
942+
/// Example 1:
943+
///
944+
/// Input: matrix = [[1,1,1],[1,0,1],[1,1,1]]
945+
/// Output: [[1,0,1],[0,0,0],[1,0,1]]
946+
///
947+
/// Example 2:
948+
///
949+
/// Input: matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
950+
/// Output: [[0,0,0,0],[0,4,5,0],[0,3,1,0]]
951+
///
952+
/// Constraints:
953+
///
954+
/// m == matrix.length
955+
/// n == matrix[0].length
956+
/// 1 <= m, n <= 200
957+
/// -231 <= matrix[i][j] <= 231 - 1
958+
///
959+
/// Follow up:
960+
///
961+
/// A straightforward solution using O(mn) space is probably a bad idea.
962+
/// A simple improvement uses O(m + n) space, but still not the best solution.
963+
/// Could you devise a constant space solution?
964+
pub fn set_zeros(matrix: &mut Vec<Vec<i32>>) {
965+
// 标记第一行/第一类是否包含0
966+
let (mut row0, mut column0) = (false, false);
967+
for i in 0..matrix.len() {
968+
for j in 0..matrix[0].len() {
969+
if matrix[i][j] == 0 {
970+
matrix[i][0] = 0;
971+
matrix[0][j] = 0;
972+
if i == 0 {
973+
row0 = true;
974+
}
975+
if j == 0 {
976+
column0 = true
977+
}
978+
}
979+
}
980+
}
981+
982+
// 遍历赋值
983+
for i in 1..matrix.len() {
984+
for j in 1..matrix[i].len() {
985+
if matrix[i][0] == 0 || matrix[0][j] == 0 {
986+
matrix[i][j] = 0;
987+
}
988+
}
989+
}
990+
if row0 {
991+
for i in 0..matrix[0].len() {
992+
matrix[0][i] = 0;
993+
}
994+
}
995+
if column0 {
996+
for i in 0..matrix.len() {
997+
matrix[i][0] = 0;
998+
}
999+
}
1000+
}
9351001
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /