1234567891011121314151617181920212223242526272829303132333435 |
- package main
- func setZeroes(matrix [][]int) {
- // Can use type 'map[int]struct{}' instead,
- // and insert empty struct like 'struct{}{}'.
- // Use 'if _, ok := m[key]; ok {}' to check.
- rows, cols := map[int]bool{}, map[int]bool{}
- for y, row := range matrix {
- for x, v := range row {
- if v == 0 {
- rows[y] = true
- cols[x] = true
- }
- }
- }
- for y, row := range matrix {
- for x := range row {
- if cols[x] || rows[y] {
- matrix[y][x] = 0
- }
- }
- }
- }
- /* func main() {
- m1 := [][]int{
- {0, 2, 4, 6},
- {1, 3, 4, 5},
- {6, 4, 0, 7},
- {4, 8, 9, 3},
- }
- setZeroes(m1)
- fmt.Println(m1)
- }
- */
|