赞
踩
《重启23》
题目
解题代码
public static int minPathSum(int[][] grid) { int width = grid[0].length, high = grid.length; if (high == 0 || width == 0) return 0; // 初始化 //往下累加 for (int i = 1; i < high; i++) { grid[i][0] += grid[i - 1][0]; } //往右累加 for (int i = 1; i < width; i++) { grid[0][i] += grid[0][i - 1]; } //把剩下的最优累加计算出来 for (int i = 1; i < high; i++) { for (int j = 1; j < width; j++) { grid[i][j] += Math.min(grid[i - 1][j], grid[i][j - 1]); } } //找到所有单个最优累加成最优解!!!!(动态规划) return grid[high - 1][width - 1]; } public static void main(String[] args) { System.out.println(minPathSum(new int[][]{new int[]{1,3,1},new int[]{1,5,1},new int[]{4,2,1}})); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。