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
}
相关推荐
叶小鸡17 小时前
小鸡玩算法-力扣HOT100-多维动态规划
算法·leetcode·动态规划
菜菜的顾清寒18 小时前
力扣HOT100(42)链表-随机链表的复制
算法·leetcode·链表
OxyTheCrack20 小时前
【Golang】简述make与new内置函数以及两者的区别
开发语言·golang
菜菜的顾清寒21 小时前
HOT力扣100(43)二叉树-翻转二叉树
数据结构·算法·leetcode
朔北之忘 Clancy1 天前
2026 年 3 月青少年软编等考 C/C++ 一级真题解析
c语言·开发语言·c++·青少年编程·题解·考级
会编程的土豆1 天前
Go 方法接收者超清晰笔记(类型名 vs 变量名)
开发语言·笔记·golang
csdn_aspnet1 天前
javascript 算法 LeetCode 编号 70 - 爬楼梯
开发语言·javascript·算法·leetcode·ecmascript
Navigator_Z1 天前
LeetCode //C - 1073. Adding Two Negabinary Numbers
c语言·算法·leetcode
csdn_aspnet1 天前
PHP 算法 LeetCode 编号 70 - 爬楼梯
算法·leetcode·php
x_xbx1 天前
LeetCode:5. 最长回文子串
算法·leetcode·职场和发展