-
Notifications
You must be signed in to change notification settings - Fork 0
/
set_matrix_0_medium.go
51 lines (47 loc) · 1.28 KB
/
set_matrix_0_medium.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
// Given a m x n matrix, if an element is 0, set its entire row and column to 0.
// Do it in-place.
// https://leetcode.com/explore/interview/card/top-interview-questions-medium/103/array-and-strings/777/
package main
import (
"log"
)
func main() {
matrix := [][]int{}
setZeroes(matrix)
log.Println(matrix)
}
func setZeroes(matrix [][]int) {
col0 := false
// remember matrix params to avoid recalc later
cols := len(matrix[0])
rows := len(matrix)
// iterate over matrix and mark columns and rows to fill with zeroes using column 0 and row 0
for i := 0; i < rows; i++ {
// column 0 is for marks so we need save if we want to rewrite it later
if matrix[i][0] == 0 {
col0 = true
}
// iterate over all columns except column 0
for j := 1; j < cols; j++ {
if matrix[i][j] == 0 {
matrix[i][0] = 0
matrix[0][j] = 0
}
}
}
// start from opposite side of the column and row choosen for marks so we can overwrite
// them in the process
for i := rows - 1; i >= 0; i-- {
// do not overwrite column 0
for j := cols - 1; j >= 1; j-- {
if matrix[0][j] == 0 || matrix[i][0] == 0 {
matrix[i][j] = 0
}
}
// set column 0 elemnt to 0 if we already processed the row
// also we can't rely on matrix as 0,0 element is abigous
if col0 {
matrix[i][0] = 0
}
}
}