-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
118 lines (106 loc) · 2.37 KB
/
main.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
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
package main
// additional memory approach
// time complexity: O(M * N) where M and N are the number
// of the rows and columns
//
// space complexity: O(M + N)
func setZeroes(matrix [][]int) {
rows := make(map[int]bool)
cols := make(map[int]bool)
for i := 0; i < len(matrix); i++ {
for j := 0; j < len(matrix[0]); j++ {
if matrix[i][j] == 0 {
rows[i], cols[j] = true, true
}
}
}
for i := 0; i < len(matrix); i++ {
for j := 0; j < len(matrix[0]); j++ {
if rows[i] || cols[j] {
matrix[i][j] = 0
}
}
}
}
// use the matrix itself as the indicators
// time complexity: O(M * N)
// space complexity: O(1)
//
// the ideas is that we can use the first cell of every row
// and column as a flag. this flag would determine whether
// a row or column has been set to zero.
func setZeroes2(matrix [][]int) {
rows, cols := len(matrix), len(matrix[0])
zeroRow, zeroCol := false, false
for row := 0; row < rows; row++ {
for col := 0; col < cols; col++ {
if matrix[row][col] == 0 {
if row == 0 {
zeroRow = true
} else {
matrix[row][0] = 0
}
if col == 0 {
zeroCol = true
} else {
matrix[0][col] = 0
}
}
}
}
for row := 1; row < rows; row++ {
for col := 1; col < cols; col++ {
if matrix[row][0] == 0 || matrix[0][col] == 0 {
matrix[row][col] = 0
}
}
}
if zeroRow {
for col := 0; col < cols; col++ {
matrix[0][col] = 0
}
}
if zeroCol {
for row := 0; row < rows; row++ {
matrix[row][0] = 0
}
}
}
// same ideas as setZeroes2
// time complexity: O(M * N)
// space complexity: O(1)
func setZeroes3(matrix [][]int) {
rows, cols := len(matrix), len(matrix[0])
zeroCol := false
for row := 0; row < rows; row++ {
if matrix[row][0] == 0 {
zeroCol = true
}
for col := 1; col < cols; col++ {
// as a flag
if matrix[row][col] == 0 {
matrix[0][col] = 0
// wether which element is zero in row[0], matrix[0][0] will be setting to zero
// so if matrix[0][0] is zero that mean all of element in the first row must be to set zero
matrix[row][0] = 0
}
}
}
for row := 1; row < rows; row++ {
for col := 1; col < cols; col++ {
if matrix[0][col] == 0 || matrix[row][0] == 0 {
matrix[row][col] = 0
}
}
}
if matrix[0][0] == 0 {
for col := 1; col < cols; col++ {
matrix[0][col] = 0
}
}
if zeroCol {
for row := 0; row < rows; row++ {
matrix[row][0] = 0
}
}
}