您的位置:首页 > 其它

LeetCOde 64. Minimum Path Sum

2016-07-28 13:39 489 查看
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.

典型动态规划问题。

public class Solution {
public int minPathSum(int[][] grid) {
int m = grid.length;
if(m<1) return 0;
int n = grid[0].length;
for(int i = 1 ; i<m; i++){
grid[i][0] = grid[i-1][0]+grid[i][0];
}

for(int j = 1; j<n; j++){
grid[0][j] = grid[0][j] + grid[0][j-1];
}

for(int i = 1; i< m; i++){
for(int j = 1; j< n; j++){
grid[i][j] += Math.min(grid[i-1][j], grid[i][j-1]);
}
}
return grid[m-1][n-1];
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode 链表