LeetCode 64 — Minimum Path Sum(C++ Java Python)
2014-02-26 22:09
711 查看
题目:http://oj.leetcode.com/problems/minimum-path-sum/
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.
题目翻译:
给定填充满非负数的m x n网格,找到从左上角到右下角的一条路径,使沿该路径的所有数字的总和最小。
注意:在任何时间点只能向下或向右移动。
分析:
类似于Unique Paths。
C++实现:
Java实现:
Python实现:
感谢阅读,欢迎评论!
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.
题目翻译:
给定填充满非负数的m x n网格,找到从左上角到右下角的一条路径,使沿该路径的所有数字的总和最小。
注意:在任何时间点只能向下或向右移动。
分析:
类似于Unique Paths。
C++实现:
class Solution { public: int minPathSum(vector<vector<int> > &grid) { int m = grid.size(); int n = grid[0].size(); int paths[m] ; paths[0][0] = grid[0][0]; for(int i = 1; i < m; ++i) { paths[i][0] = paths[i - 1][0] + grid[i][0]; } for(int j = 1; j < n; ++j) { paths[0][j] = paths[0][j - 1] + grid[0][j]; } for(int i = 1; i < m; ++i) for(int j = 1; j < n; ++j) { paths[i][j] = std::min(paths[i - 1][j], paths[i][j - 1]) + grid[i][j]; } return paths[m - 1][n - 1]; } };
Java实现:
public class Solution { public int minPathSum(int[][] grid) { int m = grid.length; int n = grid[0].length; int[][] paths = new int[m] ; paths[0][0] = grid[0][0]; for (int i = 1; i < m; ++i) { paths[i][0] = paths[i - 1][0] + grid[i][0]; } for (int j = 1; j < n; ++j) { paths[0][j] = paths[0][j - 1] + grid[0][j]; } for (int i = 1; i < m; ++i) for (int j = 1; j < n; ++j) { paths[i][j] = Math.min(paths[i - 1][j], paths[i][j - 1]) + grid[i][j]; } return paths[m - 1][n - 1]; } }
Python实现:
class Solution: # @param grid, a list of lists of integers # @return an integer def minPathSum(self, grid): m = len(grid) n = len(grid[0]) paths = [[] for i in range(m)] paths[0].append(grid[0][0]) for i in range(1, m): paths[i].append(paths[i - 1][0] + grid[i][0]) for j in range(1, n): paths[0].append(paths[0][j - 1] + grid[0][j]) for i in range(1, m): for j in range(1, n): paths[i].append(min(paths[i][j - 1], paths[i - 1][j]) + grid[i][j]) return paths[m - 1][n - 1]
感谢阅读,欢迎评论!
相关文章推荐
- LeetCode 112 — Path Sum(C++ Java Python)
- LeetCode 64 Minimum Path Sum(Python详解及代码实现)
- 【python】【leetcode】【算法题目64—Minimum Path Sum】
- Leetcode-64_. Minimum Path Sum(最小路径和)—动态规划解法+记忆化搜索解法-C++解
- leetcode 64 Minimum Path Sum C++
- (java)leetcode-64:Minimum Path Sum
- [LeetCode][Java] Minimum Path Sum
- 【LeetCode-面试算法经典-Java实现】【064-Minimum Path Sum(最小路径和)】
- leetcode-64:Minimum Path Sum
- [Leetcode-64]Minimum Path Sum 二维数组路径最小和
- leetcode64 Minimum Path Sum
- [Leetcode 64, Medium] Minimum Path Sum
- leetcode JAVA Minimum Path Sum 难度系数3 3.22
- Leetcode_path-sum (updated c++ and python version)
- 【LEETCODE】64-Minimum Path Sum
- LeetCode64——Minimum Path Sum
- leetcode 64 c语言. Minimum Path Sum
- leetcode64-Minimum Path Sum(最小路径和)
- LeetCode 64: Minimum Path Sum
- leetcode_question_64 Minimum Path Sum