73.go 618 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func setZeroes(matrix [][]int) {
  6. // Can use type 'map[int]struct{}' instead,
  7. // and insert empty struct like 'struct{}{}'.
  8. // Use 'if _, ok := m[key]; ok {}' to check.
  9. rows, cols := map[int]bool{}, map[int]bool{}
  10. for y, row := range matrix {
  11. for x, v := range row {
  12. if v == 0 {
  13. rows[y] = true
  14. cols[x] = true
  15. }
  16. }
  17. }
  18. for y, row := range matrix {
  19. for x := range row {
  20. if cols[x] || rows[y] {
  21. matrix[y][x] = 0
  22. }
  23. }
  24. }
  25. }
  26. func main() {
  27. m1 := [][]int{
  28. {0, 2, 4, 6},
  29. {1, 3, 4, 5},
  30. {6, 4, 0, 7},
  31. {4, 8, 9, 3},
  32. }
  33. setZeroes(m1)
  34. fmt.Println(m1)
  35. }