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
        }
    }
}
相关推荐
Nil208几秒前
leetcode 45跳跃游戏Ⅱ
算法·leetcode·游戏
王老师青少年编程17 分钟前
2026年6月GESP真题及题解(C++八级): 选择题和判断题(题解)
c++·题解·真题·gesp·csp·信奥赛·八级
longlongzihan23 分钟前
LeetCode 283. 移动零:从辅助数组到双指针原地算法
c++·算法·leetcode
longlongzihan14 小时前
LeetCode 5. 最长回文子串:从暴力匹配到中心扩展的思维演进
c++·算法·leetcode
苦瓜打怪兽14 小时前
leetcode 热题100(11. 盛最多水的容器)
python·算法·leetcode
microrain17 小时前
同一个寄存器,四种读法:SagooIoT Modbus 驱动的字节序、宽度与功能码
物联网·golang·开源·sagooiot
JWASX19 小时前
Java 转 go 学习 - 并发编程(2)
学习·golang
All for pursuit.19 小时前
【回溯-6】79.单词搜索
数据结构·c++·算法·leetcode
Navigator_Z19 小时前
LeetCode //C - 1281. Subtract the Product and Sum of Digits of an Integer
c语言·算法·leetcode
ttwuai20 小时前
Go开源后台管理系统推荐:README、源码和License应该怎么验
开发语言·golang·开源