码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode: Triangle

时间:2014-05-30 18:22:50      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

 这道题还是挺难的一道题,想法很重要

 

bubuko.com,布布扣
 1 public class Solution {
 2     public int minimumTotal(List<List<Integer>> triangle) {
 3         int size = triangle.size();
 4         int[] leveldistances = new int[size];
 5         int[] store = new int[size];
 6         for (int i = 0; i < size; i++) {
 7             for (int j = 0; j <= i; j++) {
 8                 List<Integer> row = triangle.get(i);
 9                 if (i == 0) {
10                     leveldistances[0] = row.get(0);
11                 } 
12                 else if (j == 0) {
13                     leveldistances[j] = store[j] + row.get(j);
14                 }
15                 else if (j == i) {
16                     leveldistances[j] = store[j-1] + row.get(j);
17                 }
18                 else {
19                     leveldistances[j] = Math.min(store[j-1], store[j]) + row.get(j);
20                 }
21             }
22             store = Arrays.copyOf(leveldistances, size);
23         }
24         int minval = Integer.MAX_VALUE;
25         for (int elem : leveldistances) {
26             if (elem < minval) minval = elem;
27         }
28         return minval;
29     }
30 }
bubuko.com,布布扣

 

Leetcode: Triangle,布布扣,bubuko.com

Leetcode: Triangle

标签:c   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/EdwardLiu/p/3760304.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!