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
        }
    }
}
相关推荐
Kuo-Teng7 小时前
LeetCode 160: Intersection of Two Linked Lists
java·算法·leetcode·职场和发展
码上淘金9 小时前
在 YAML 中如何将 JSON 对象作为字符串整体赋值?——兼谈 Go Template 中的 fromJson 使用
java·golang·json
橘颂TA10 小时前
【剑斩OFFER】算法的暴力美学——点名
数据结构·算法·leetcode·c/c++
愚润求学14 小时前
【动态规划】专题完结,题单汇总
算法·leetcode·动态规划
·白小白15 小时前
力扣(LeetCode) ——43.字符串相乘(C++)
c++·leetcode
小生凡一15 小时前
图解|Go语言实现 Agent|LLM+MCP+RAG
开发语言·后端·golang
pipip.17 小时前
Go原生高性能内存网关IMS,比Redis更快
开发语言·redis·golang
一匹电信狗19 小时前
【C++11】Lambda表达式+新的类功能
服务器·c++·算法·leetcode·小程序·stl·visual studio
在等晚安么19 小时前
力扣面试150题打卡
算法·leetcode·面试
q***062919 小时前
环境安装与配置:全面了解 Go 语言的安装与设置
开发语言·后端·golang