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
}
相关推荐
apocelipes4 小时前
下划线字段在golang结构体中的应用
golang
一匹电信狗14 小时前
【C++】异常详解(万字解读)
服务器·c++·算法·leetcode·小程序·stl·visual studio
Python私教15 小时前
从“Hello World”到“高并发中间件”:Go 语言 2025 系统学习路线图
学习·中间件·golang
墨染点香15 小时前
LeetCode 刷题【43. 字符串相乘】
算法·leetcode·职场和发展
Keying,,,,16 小时前
力扣hot100 | 矩阵 | 73. 矩阵置零、54. 螺旋矩阵、48. 旋转图像、240. 搜索二维矩阵 II
python·算法·leetcode·矩阵
_不会dp不改名_17 小时前
leetcode_42 接雨水
算法·leetcode·职场和发展
code小毛孩20 小时前
leetcode hot100数组:缺失的第一个正数
数据结构·算法·leetcode
快去睡觉~1 天前
力扣400:第N位数字
数据结构·算法·leetcode
gzzeason1 天前
LeetCode Hot100:递归穿透值传递问题
算法·leetcode·职场和发展
光爷不秃1 天前
Go语言中安全停止Goroutine的三种方法及设计哲学
开发语言·安全·golang