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
原文地址:https://www.cnblogs.com/sunshisonghit/p/4335913.html