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]
]

一开始的题目的意思理解错了 ,以为是位数相差一就是临近的意思,但实际上这里意思是图形上面的那种临近,和原来那个实际上是不一样的。用到了dfs,我一开始还想用一个min的二位vector来保存结果

但是实际上不用,直接用原来的triangle数组就可以保存结果了:

 class Solution {
public:
int minimumTotal(vector<vector<int>>& triangle) {
if(triangle.size() == || triangle[].size() == )
return ;
//vector<vector<int>>minRet(triangle.size(), vector<int>(triangle[0].size(), 0));
for(int i = ; i < triangle.size(); ++i){ // i从1开始
for(int j = ; j < triangle[i].size(); ++j){
if(j == ){
triangle[i][j] += triangle[i - ][];
}else if(j == triangle[i].size() - ){
triangle[i][j] += triangle[i - ][j - ];
}else{
triangle[i][j] += min(triangle[i - ][j - ], triangle[i - ][j]);
}
}
}
int horSz = triangle.size();
int sz = triangle[horSz - ].size();
int ret = triangle[horSz - ][];
for(int i = ; i < sz; ++i){
if(ret > triangle[horSz - ][i])
ret = triangle[horSz - ][i];
}
return ret;
}
};

这个题用java写还是略坑爹,各种get,add写起来感觉好麻烦,暂时就不写了。

LeetCode OJ:Triangle(三角形)的更多相关文章

  1. LeetCode 120. Triangle三角形最小路径和 (C++)

    题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...

  2. LeetCode 120. Triangle (三角形最小路径和)详解

    题目详情 给定一个三角形,找出自顶向下的最小路径和.每一步只能移动到下一行中相邻的结点上. 例如,给定三角形: [ [2], [3,4], [6,5,7], [4,1,8,3] ] 自顶向下的最小路径 ...

  3. Leetcode OJ : Triangle 动态规划 python solution

    Total Accepted: 31557 Total Submissions: 116793     Given a triangle, find the minimum path sum from ...

  4. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  5. 【LeetCode OJ】Interleaving String

    Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...

  6. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

  7. LeetCode OJ学习

    一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...

  8. LeetCode OJ 297. Serialize and Deserialize Binary Tree

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  9. 备份LeetCode OJ自己编写的代码

    常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...

  10. LeetCode OJ 之 Maximal Square (最大的正方形)

    题目: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ...

随机推荐

  1. Ubantu安装Odoo10学习日志

    Windows安装是十分简单的,无论是何种方式,在了解大体情况下,我开始尝试下将Odoo搭建部署在Ubantu上. 1.Odoo是什么? Odoo是一个开源框架,针对ERP的需求发展出来,适合定制出符 ...

  2. 007-搭建框架-开发AOP框架

    一.代码地址 https://github.com/bjlhx15/smart-framework.git 二.代码编写 2.1.定义切面注解 增加Aspect注解 package com.lhx.s ...

  3. zabbix-2.4.8-1添加MySQL状态监控

    1.安装zabbix-agentyum -y install zabbix-2.4.8-1.el6.x86_64.rpm zabbix-agent-2.4.8-1.el6.x86_64.rpm 安装以 ...

  4. SaltStack远程执行命令

    编辑fansik_cmd.sls文件: 内容如下: fansik_cmd:  cmd.run:    - unless:      - test -f /tmp/fansik.txt      - t ...

  5. Python函数之—— 装饰器(Day13)

    一.什么是装饰器 顾名思义,装饰器指为其他函数添加新功能 装饰器定义:本质就是函数,功能是为其他函数添加新功能 二.装饰器需要遵循的原则 1.不修改被装饰函数的源代码(开放封闭原则) 2.为被装饰函数 ...

  6. python删除列表中所有的空元素

    while '' in list: list.remove('')

  7. yii 2 局部关闭 CSRF 拦截

    最近在拿 yii 2.0 开发ajax提交,在 post 请求接口时,提示数据无法验证,于是查询 yii 错误日志,发现错误为 exception ‘yiiwebBadRequestHttpExcep ...

  8. 建议37:按需选择sort或sorted

    # -*- coding:utf-8 -*- ''' 用法: sorted(iterable[, cmp[, key[, reverse]]]) s.sort([cmp[, key[, reverse ...

  9. JSP笔记04——架构(转)

    原始内容:https://www.tutorialspoint.com/struts_2/basic_mvc_architecture.htm Web服务器需要一个JSP引擎,即一个处理JSP页面的容 ...

  10. 0801 RESTAPI设计,DRF 序列化

    1.内容回顾    1.restframework serializer(序列化)的简单使用                QuereySet([obj,obj,obj])  -->  JSON ...