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
}
相关推荐
想搞艺术的程序员24 分钟前
Go 语言 interface 详解:从源码到实践
golang·interface
foxsen_xia32 分钟前
go(基础01)——协程
开发语言·算法·golang
源代码•宸34 分钟前
GoLang并发简单例子(goroutine + channel + WaitGroup)
开发语言·经验分享·后端·学习·golang
稚辉君.MCA_P8_Java36 分钟前
Gemini永久会员 Go 返回最长有效子串长度
数据结构·后端·算法·golang
jyyyx的算法博客39 分钟前
LeetCode 面试题 16.22. 兰顿蚂蚁
算法·leetcode
Q741_1471 小时前
C++ 高精度计算的讲解 模拟 力扣67.二进制求和 题解 每日一题
c++·算法·leetcode·高精度·模拟
leoufung1 小时前
LeetCode 98 Validate Binary Search Tree 深度解析
算法·leetcode·职场和发展
jyyyx的算法博客1 小时前
LeetCode 面试题 16.18. 模式匹配
算法·leetcode
前端之虎陈随易2 小时前
基于Go重写的TypeScript 7可以用了
开发语言·golang·typescript
ada7_2 小时前
LeetCode(python)——94.二叉
python·算法·leetcode·链表·职场和发展