美文网首页
剑指 Offer II 099. 最小路径之和

剑指 Offer II 099. 最小路径之和

作者: 邦_ | 来源:发表于2022-07-13 09:30 被阅读0次
    
    
    func minPathSum(_ grid: [[Int]]) -> Int {
    
            let row = grid.count
            let col = grid.first?.count ?? 0
            let temp = Array.init(repeating: 0, count: col)
            var dp = Array.init(repeating: temp, count: row)
            for i in 0..<row{
                
                for j in 0..<col {
                    
                    if i == 0 && j == 0{
                        dp[0][0] = grid[0][0]
                    }else if i == 0 {
                        dp[0][j] += dp[0][j - 1] + grid[i][j]
                    }else if j == 0 {
                        dp[i][0] = dp[i - 1][0] + grid[i][j]
                    }else {
                        dp[i][j] = min(dp[i - 1][j] , dp[i][j - 1]) + grid[i][j]
                    }
                    
                }
            }
            
            return dp[row - 1][col - 1]
        
        }
    
    
    
    
    
    
    
    
    

    相关文章

      网友评论

          本文标题:剑指 Offer II 099. 最小路径之和

          本文链接:https://www.haomeiwen.com/subject/vmdvbrtx.html