1 public class Solution { 2 /** 3 * @param triangle: a list of lists of integers. 4 * @return: An integer, minimum path sum. 5 */ 6 public int minimumTotal(int[][] triangle) { 7 // write your code here 8 if(triangle.length == 1 && triangle[0].length ==1 ){ 9 return triangle[0][0]; 10 } 11 int minnum = 0; 12 for(int i = 1; i < triangle.length ; i++){ 13 for(int j = 0;j < triangle[i].length ; j++){ 14 if(j==0){ 15 triangle[i][j] += triangle[i-1][j]; 16 }else if(j==(triangle[i].length - 1)){ 17 triangle[i][j] += triangle[i-1][j-1]; 18 }else{ 19 triangle[i][j] += Math.min(triangle[i-1][j],triangle[i-1][j-1]); 20 } 21 if(i == (triangle.length - 1) && j==0){ 22 minnum = triangle[i][j]; 23 }else{ 24 minnum = Math.min(minnum,triangle[i][j]); 25 } 26 } 27 } 28 return minnum; 29 } 30 }