public class 最小路径和 { public int minPathSum(int[][] grid) { int[] dp = new int[grid[0].length]; dp[0] = grid[0][0]; for (int i = grid.length - 1; i >= 0; i--) { for (int j = grid[0].length; j >= 0; j--) { if (i == grid.length - 1 && j != grid[0].length - 1) { dp[j] = grid[i][j] + dp[j + 1]; } else if (i != grid.length - 1 && j == grid[0].length - 1) { dp[j] = grid[i][j] + dp[j]; } else if (i != grid.length - 1 && j != grid[0].length - 1) { dp[j] = grid[i][j] + Math.min(dp[j],dp[j + 1]); } else { dp[j] = grid[i][j]; } } } return dp[0]; } }