Triangle

Triangle

问题:

Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.

思路:

  简单的动态规划问题

我的代码:

public class Solution {
    public int minimumTotal(List<List<Integer>> triangle) {
        if(triangle == null || triangle.size() == 0)    return -1;
        int size = triangle.size();
        for(int i = size - 2; i >= 0; i--)
        {
            List<Integer> cur = triangle.get(i);
            List<Integer> pre = triangle.get(i+1);
            for(int j = 0; j < cur.size(); j++)
            {
                cur.set(j, cur.get(j) + Math.min(pre.get(j),pre.get(j+1)));
            }
        }
        return triangle.get(0).get(0);
    }
}
View Code

 

posted on 2015-03-13 19:52  zhouzhou0615  阅读(113)  评论(0编辑  收藏  举报

导航