首页 > 代码库 > [LeetCode]64 Minimum Path Sum
[LeetCode]64 Minimum Path Sum
https://oj.leetcode.com/problems/minimum-path-sum/
http://blog.csdn.net/linhuanmars/article/details/22257673
public class Solution { public int minPathSum(int[][] grid) { // Only move down or right. // Otherwise, -oo // Validations. if (grid == null) return -1; // Invalid input int m = grid.length; int n = grid[0].length; int[][] pathsum = new int[m][n]; for (int i = 0 ; i < m ; i ++) { for (int j = 0 ; j < n ; j ++) { int v = 0; if (i == 0 && j == 0) { v = grid[i][j]; } else if (i == 0) { v = pathsum[i][j - 1] + grid[i][j]; } else if (j == 0) { v = pathsum[i - 1][j] + grid[i][j]; } else { v = Math.min(pathsum[i][j - 1], pathsum[i - 1][j]) + grid[i][j]; } pathsum[i][j] = v; } } return pathsum[m - 1][n - 1]; } }
[LeetCode]64 Minimum Path Sum
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。