set_matrix_zeros

  • 2022-12-14
  • 浏览 (522)

set_matrix_zeros.go 源码

package main

// 记录0出现的位置,然后再遍历置位0
func setZeroes1(matrix [][]int) {
	hor, ver := make(map[int]bool), make(map[int]bool)
	for i := 0; i < len(matrix); i++ {
		for j := 0; j < len(matrix[i]); j++ {
			if matrix[i][j] == 0 {
				hor[i], ver[j] = true, true
			}
		}
	}

	for i := 0; i < len(matrix); i++ {
		for j := 0; j < len(matrix[i]); j++ {
			if hor[i] || ver[j] {
				matrix[i][j] = 0
			}
		}
	}
}

func setZeroes2(matrix [][]int) {
	for i := 0; i < len(matrix); i++ {
		for j := 0; j < len(matrix[i]); j++ {
			if matrix[i][j] == 0 {
				helper(i, j, matrix)
			}
		}
	}

	for i := 0; i < len(matrix); i++ {
		for j := 0; j < len(matrix[i]); j++ {
			if matrix[i][j] == '*' {
				matrix[i][j] = 0
			}
		}
	}
}

func helper(i, j int, matrix [][]int) {
	for k := 0; k < len(matrix); k++ {
		if matrix[k][j] != 0 {
			matrix[k][j] = '*'
		}
	}

	for k := 0; k < len(matrix[i]); k++ {
		if matrix[i][k] != 0 {
			matrix[i][k] = '*'
		}
	}
}

你可能感兴趣的文章

best_time_buy_sell_stock

best_time_buy_sell_stock_ii

climb_stairs

0  赞