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. excel同时冻结首行和首列怎么操作

    之前ytkah只知道excel可以冻结首行或首列,但还不清楚如何同时冻结excel首行和首列,后面看到小C的报表,问了他才明白怎么操作. 首先,我们先把选中B2单元格,点击导航菜单的“视图” - “冻 ...

  2. Redis分布式锁的python实现

    案例1: #!/usr/bin/env python # coding=utf-8 import time import redis class RedisLock(object): def __in ...

  3. MFC实现文字随鼠标移动

    1 实验介绍 此实验是在刚开始接触MFC时做的.它要求实现的功能如下: 文字跟随鼠标动态移动(跟随移动方式自定) 修改图标为自己喜欢的图标 修改窗口标题 修改文档名称 可以用菜单项选定指定的颜色显示文 ...

  4. ABP框架数据迁移报错

    问题描述:将项目从TFS载下来  然后敲update-database 进行数据迁移 提示:Update-Database : 无法将“Update-Database”项识别为 cmdlet.函数.脚 ...

  5. bug营销手段

    肯德基搞事了. 几乎是一夜之间,肯德基App就杀进了iOS热门App排行榜的前50名. 一夜之间排名猛增,一定是事出有因的.那这次的原因是什么? bug. 肯德基App出现了一个大bug,用户将账号生 ...

  6. Oracle索引(2)索引的修改与维护

    修改索引   利用alter index语句可以完成的操作 重建或合并索引 回收索引未使用的空间或为索引非配新空间 修改索引是否可以并行操作及并行度 修改索引的存储参数以及物理属性 指定Logging ...

  7. $Android中日期和时间选择器的实现

    创建日期或时间选择窗口需要弹出Dialog的时候,Activity类的showDialog方法已经弃用了,而推荐使用的是DialogFragment,本文总结一下其具体用法. (一)日期选择器 1.创 ...

  8. 012_Eclipse中使用 HDFS URL API 事例介绍

    本事例其实和使用hdfs FileSystem API差不多,FileSystem API也是通过解释成URL在hdfs上面执行的,性质相同,但是实际中用 的fFileSystem会多一点,源码如下: ...

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

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

  10. BEM(一种 CSS 命名规则)

    一. 什么是 BEM BEM的意思就是块(block).元素(element).修饰符(modifier),是由 Yandex 团队提出的一种前端命名方法论. 这种巧妙的命名方法让你的 CSS 类对其 ...