Golang | Leetcode Golang题解之第64题最小路径和

题目:

题解:

Go 复制代码
func minPathSum(grid [][]int) int {
    if len(grid) == 0 || len(grid[0]) == 0 {
        return 0
    }
    rows, columns := len(grid), len(grid[0])
    dp := make([][]int, rows)
    for i := 0; i < len(dp); i++ {
        dp[i] = make([]int, columns)
    }
    dp[0][0] = grid[0][0]
    for i := 1; i < rows; i++ {
        dp[i][0] = dp[i - 1][0] + grid[i][0]
    }
    for j := 1; j < columns; j++ {
        dp[0][j] = dp[0][j - 1] + grid[0][j]
    }
    for i := 1; i < rows; i++ {
        for j := 1; j < columns; j++ {
            dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j]
        }
    }
    return dp[rows - 1][columns - 1]
}

func min(x, y int) int {
    if x < y {
        return x
    }
    return y
}
相关推荐
鱼儿也有烦恼2 分钟前
快速学完 LeetCode top 1~50
leetcode·algorithm
李燚4 小时前
Eino devops 调试系统源码:GraphCompileCallback 怎么工作(第50篇-E36)
数据库·golang·agent·devops·graphql·aiagent·eino
退休倒计时4 小时前
【每日一题】LeetCode 17. 电话号码的字母组合 TypeScript
算法·leetcode·typescript
临沂堇5 小时前
刷题日志 | LeetCode Hot 100 双指针
算法·leetcode·职场和发展
XWalnut5 小时前
LeetCode刷题 day29
java·算法·leetcode
名字还没想好☜7 小时前
Go 内存逃逸分析:什么时候变量跑到堆上
开发语言·算法·性能优化·golang·go·内存逃逸
小欣加油7 小时前
leetcode1331 数组序号转换
数据结构·c++·算法·leetcode·职场和发展
ttwuai8 小时前
后台管理系统 RBAC 排障:隐藏菜单后接口为什么还能调用
golang
Hesionberger9 小时前
动态规划与二分法破解最长递增子序列
java·数据结构·python·算法·leetcode
令狐掌门10 小时前
2026华为OD面试题020:日志文件异常检测
算法·leetcode·华为od