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. nginx基础系列

    centos中搭建nginx环境 nginx开机启动 nginx配置文件说明 nginx负载均衡配置 nginx upstream模块 nginx配置ssl nginx日志切割 nginx平滑升级

  2. java上机

    package wordcont; import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayLi ...

  3. func && operation_yes || operation_no (Shell)

    通过&&, || 理解shell中的函数返回值. 我想实现如下功能: 写一个函数判断一个字符串中是否只包含数字,并返回相应的标志(是/否); 通过调用上面的函数,判断给定的字符串是否只 ...

  4. PAT 天梯赛 L1-039. 古风排版 【字符串处理】

    题目链接 https://www.patest.cn/contests/gplt/L1-039 思路 先根据 len 和 n 判断 有几个 列和几行,然后 从最右边 到 最左边 从上到下 将字符串 录 ...

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

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

  6. PHP用*隐藏中文问题

    中文截取 function substr_cut($user_name){ $strlen= mb_strlen($user_name, 'utf-8'); $firstStr= mb_substr( ...

  7. PHP中的常见魔术方法功能作用及用法实例

    概述 在面向对象编程中,PHP提供了一系列的魔术方法,这些魔术方法为编程提供了很多便利.PHP中的魔术方法通常以__(两个下划线)开始,并且不需要显示的调用而是由某种特定的条件出发. 开始之前 在总结 ...

  8. Kubernetes Rook

    Rook Rook 是一个开源的cloud-native storage编排, 提供平台和框架:为各种存储解决方案提供平台.框架和支持,以便与云原生环境本地集成. Rook 将存储软件转变为自我管理. ...

  9. asp.net 5 (mvc 6) 获取网站的物理路径

    public static IApplicationEnvironment GetApplication(this RazorPage page) { var ae = page.Context.Re ...

  10. poj 3468 线段树模板题

    #include<iostream> #include<algorithm> #include<stdio.h> using namespace std; #def ...