矩阵置零
// 矩阵置零
package matrix
func setZeroes(matrix [][]int) {
rows := len(matrix)
cols := len(matrix[0])
rowFlags := make([]bool, rows)
colFlags := make([]bool, cols)
// 遍历矩阵,标记需要设置为0的行和列
for i := 0; i < rows; i++ {
for j := 0; j < cols; j++ {
if matrix[i][j] == 0 {
rowFlags[i] = true
colFlags[j] = true
}
}
}
// 根据标记设置行和列为0
for i := 0; i < rows; i++ {
for j := 0; j < cols; j++ {
if rowFlags[i] || colFlags[j] {
matrix[i][j] = 0
}
}
}
}