题解 | #三角形最小路径和#
三角形最小路径和
https://www.nowcoder.com/practice/c9d44b73dc7c4dbfa4272224b1f9b42c
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param triangle int整型二维数组 * @return int整型 */ public int minTrace (int[][] triangle) { // write code here int height = triangle.length; int min = Integer.MAX_VALUE; int res[][] = new int[height][height]; res[0][0] = triangle[0][0]; for (int i = 1; i < height; i++) { for (int j = 0; j < i + 1; j++) { if (j == 0) { res[i][j] = res[i - 1][j] + triangle[i][j]; } else if (j == i) { res[i][j] = res[i - 1][j - 1] + triangle[i][j]; } else { res[i][j] = Math.min(res[i - 1][j - 1], res[i - 1][j]) + triangle[i][j]; } } } for (int j = 0; j < height; j++) { int temp = res[height - 1][j]; if (temp < min) { min = temp; } } return min; } }