Golang | Leetcode Golang题解之第73题矩阵置零

题目:

题解:

Go 复制代码
func setZeroes(matrix [][]int) {
    n, m := len(matrix), len(matrix[0])
    col0 := false
    for _, r := range matrix {
        if r[0] == 0 {
            col0 = true
        }
        for j := 1; j < m; j++ {
            if r[j] == 0 {
                r[0] = 0
                matrix[0][j] = 0
            }
        }
    }
    for i := n - 1; i >= 0; i-- {
        for j := 1; j < m; j++ {
            if matrix[i][0] == 0 || matrix[0][j] == 0 {
                matrix[i][j] = 0
            }
        }
        if col0 {
            matrix[i][0] = 0
        }
    }
}
相关推荐
CoderYanger16 小时前
优选算法-栈:67.基本计算器Ⅱ
java·开发语言·算法·leetcode·职场和发展·1024程序员节
夏鹏今天学习了吗16 小时前
【LeetCode热题100(72/100)】前 K 个高频元素
leetcode
百***480717 小时前
【Golang】slice切片
开发语言·算法·golang
q***925118 小时前
Windows上安装Go并配置环境变量(图文步骤)
开发语言·windows·golang
墨染点香18 小时前
LeetCode 刷题【172. 阶乘后的零】
算法·leetcode·职场和发展
做怪小疯子18 小时前
LeetCode 热题 100——链表——反转链表
算法·leetcode·链表
做怪小疯子20 小时前
LeetCode 热题 100——矩阵——旋转图像
算法·leetcode·矩阵
sin_hielo21 小时前
leetcode 2435
数据结构·算法·leetcode
稚辉君.MCA_P8_Java1 天前
Gemini永久会员 Java动态规划
java·数据结构·leetcode·排序算法·动态规划
小白程序员成长日记1 天前
2025.11.23 力扣每日一题
算法·leetcode·职场和发展