73.go 605 B

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