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
}
相关推荐
吃着火锅x唱着歌17 小时前
LeetCode 74.搜索二维矩阵
算法·leetcode·矩阵
golang学习记17 小时前
Go slog 日志打印最佳实践指南
开发语言·后端·golang
linff91118 小时前
hot 100 技巧题
数据结构·算法·leetcode
Miraitowa_cheems1 天前
LeetCode算法日记 - Day 84: 乘积为正数的最长子数组长度
数据结构·算法·leetcode·贪心算法·线性回归·深度优先·动态规划
xiaopengbc1 天前
谷歌商店下载APK教程,先下载谷歌三件套,再直接从 Google Play 下载 APK 文件?
leetcode
水淹萌龙1 天前
玩转 Go 表达式引擎:expr 实战指南
开发语言·后端·golang
Yeats_Liao1 天前
Go Web 编程快速入门 07.4 - 模板(4):组合模板与逻辑控制
开发语言·后端·golang
Dream it possible!1 天前
LeetCode 面试经典 150_链表_反转链表 II(60_92_C++_中等)(头插法)
c++·leetcode·链表·面试
nexttake1 天前
5.go-zero集成gorm 和 go-redis
开发语言·后端·golang
py有趣1 天前
LeetCode学习之实现strStr()
学习·算法·leetcode