博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【Leetcode】120.Triangle
阅读量:4359 次
发布时间:2019-06-07

本文共 758 字,大约阅读时间需要 2 分钟。

[Question]

 

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

For example, given the following triangle

[     [2],    [3,4],   [6,5,7],  [4,1,8,3]]

The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11).

 

[Solution]

//C++class Solution {public:    int minimumTotal(vector& triangle) {        if(triangle.size()==0)            return 0;        if(triangle.size()==1)            return(triangle[0][0]);        for(int i=triangle.size()-2; i>=0; i--){            for(int j=0; j<=i; j++){                triangle[i][j] += min(triangle[i+1][j], triangle[i+1][j+1]);            }        }        return triangle[0][0];    }};

 

转载于:https://www.cnblogs.com/liez/p/5951358.html

你可能感兴趣的文章
七尖记
查看>>
SAP(最短增广路算法) 最大流模板
查看>>
安装 OpenSSL 工具
查看>>
用长微博工具发布长微博
查看>>
大庆金桥帆软报表案例
查看>>
Proxy模式
查看>>
读书多些会怎样
查看>>
浏览器好用的技术
查看>>
HDU 2188------巴什博弈
查看>>
tp5任务队列使用supervisor常驻进程
查看>>
Xmind?
查看>>
spring+quartz 实现定时任务三
查看>>
day2-三级菜单
查看>>
linux下升级4.5.1版本gcc
查看>>
Beanutils
查看>>
FastJson
查看>>
excel4j
查看>>
Thread
查看>>
char * 与char []探究理解
查看>>
QT窗体显示在屏幕中间位置
查看>>