LeetCode 64. Minimum Path Sum(Python)
来源:互联网 发布:淘宝客服转正评估表 编辑:程序博客网 时间:2024/06/09 21:14
题目描述:
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.
Note: You can only move either down or right at any point in
time.
思路:
本题求左上角到右下角的最短路径,因为只允许向下或向右操作,所以该问题可以化简为(i, j)处(i > 0, j > 0)的最短路径就是(i - 1, j)和(i, j - 1)处的较小值加上(i, j)的value
AC代码:
class Solution(object): def minPathSum(self, grid): """ :type grid: List[List[int]] :rtype: int """ res = [[0 for i in range(len(grid[0]))] for i in range(len(grid))] for i in range(len(grid)): for j in range(len(grid[0])): if i == 0: res[i][j] = res[i][j - 1] + grid[i][j] elif j == 0: res[i][j] = res[i - 1][j] + grid[i][j] else: res[i][j] = min(res[i - 1][j], res[i][j - 1]) + grid[i][j] return res[-1][-1]
阅读全文
0 0
- LeetCode 64. Minimum Path Sum(Python)
- 【LeetCode】【Python】Minimum Path Sum
- 【Leetcode】【python】Minimum Path Sum
- [LeetCode]64.Minimum Path Sum
- LeetCode --- 64. Minimum Path Sum
- LeetCode 64.Minimum Path Sum
- [Leetcode] 64. Minimum Path Sum
- [leetcode] 64.Minimum Path Sum
- [leetcode] 64.Minimum Path Sum
- LeetCode 64. Minimum Path Sum
- 64. Minimum Path Sum LeetCode
- [LeetCode]64. Minimum Path Sum
- leetcode 64. Minimum Path Sum
- [leetcode] 64. Minimum Path Sum
- LeetCode 64. Minimum Path Sum
- LeetCode *** 64. Minimum Path Sum
- leetcode 64. Minimum Path Sum
- 【leetcode】64. Minimum Path Sum
- Android简易音乐播放器实现代码
- LeetCode No.86 Partition List
- Orleans稍微复杂的例子—互动
- Android--常用控件
- 最小的k个数
- LeetCode 64. Minimum Path Sum(Python)
- Lucene第一天入门
- Android如何实现简单音乐播放器的代码
- Scala基础入门(十 ) Scala for循环使用方法
- 无向图寻找是否存在长度为k的环
- 在VMware-workstation12(虚拟机)上安装uduntu16.04(linux系统)和VMware Tools教程
- 关于洛古p1019单次接龙的一点看法
- ZOJ 2002 Copying Books 二分 贪心
- 欢迎使用CSDN-markdown编辑器