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
}
相关推荐
_深海凉_12 分钟前
LeetCode热题100-回文链表
算法·leetcode·链表
小雅痞15 分钟前
[Java][Leetcode middle] 54. 螺旋矩阵
java·leetcode·矩阵
pursuit_csdn18 分钟前
力扣周赛 501
算法·leetcode·职场和发展
AbandonForce20 分钟前
LeetCode 滑动窗口个人思路详解
算法·leetcode·职场和发展
金玉满堂@bj20 分钟前
Go 语言能做什么?
开发语言·后端·golang
Liangwei Lin22 分钟前
LeetCode 45. 跳跃游戏 II
数据结构·算法·leetcode
geovindu1 小时前
go:Condition Variable Pattern
开发语言·后端·设计模式·golang·条件变量模式
金玉满堂@bj1 小时前
Gin 框架零基础全套入门教程(Go 企业级 Web 开发)
前端·golang·gin
OYangxf1 小时前
力扣hot100【子串专题】
算法·leetcode·职场和发展
玛卡巴卡ldf3 小时前
【LeetCode 手撕算法】(二分查找)搜索插入位置、搜索二维矩阵、查找数组相同的所有位置、搜索旋转排序数组、旋转升序数组的最小值
数据结构·算法·leetcode