/
73.set-matrix-zeroes.go
64 lines (57 loc) · 1.06 KB
/
73.set-matrix-zeroes.go
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
/*
* @lc app=leetcode id=73 lang=golang
*
* [73] Set Matrix Zeroes
*/
package golang
// @lc code=start
func setZeroes(matrix [][]int) {
X, Y := len(matrix), len(matrix[0])
// row, col := checkZero(X, Y, matrix)
// setZero(X, Y, col, matrix, row)
col0 := false
for i := 0; i < X; i++ {
if matrix[i][0] == 0 {
col0 = true
}
for j := 1; j < Y; j++ {
if matrix[i][j] == 0 {
matrix[0][j] = 0
matrix[i][0] = 0
}
}
}
for i := X - 1; i >= 0; i-- {
for j := Y - 1; j >= 1; j-- {
if matrix[0][j] == 0 || matrix[i][0] == 0 {
matrix[i][j] = 0
}
}
if col0 {
matrix[i][0] = 0
}
}
}
func setZero(X int, Y int, col []bool, matrix [][]int, row []bool) {
for i := 0; i < X; i++ {
for j := 0; j < Y; j++ {
if row[i] || col[j] {
matrix[i][j] = 0
}
}
}
}
func checkZero(X int, Y int, matrix [][]int) ([]bool, []bool) {
row := make([]bool, X)
col := make([]bool, Y)
for i := 0; i < X; i++ {
for j := 0; j < Y; j++ {
if matrix[i][j] == 0 {
row[i] = true
col[j] = true
}
}
}
return row, col
}
// @lc code=end