LeetCode OJ: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.
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(三角形)的更多相关文章
- LeetCode 120. Triangle三角形最小路径和 (C++)
题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...
- LeetCode 120. Triangle (三角形最小路径和)详解
题目详情 给定一个三角形,找出自顶向下的最小路径和.每一步只能移动到下一行中相邻的结点上. 例如,给定三角形: [ [2], [3,4], [6,5,7], [4,1,8,3] ] 自顶向下的最小路径 ...
- Leetcode OJ : Triangle 动态规划 python solution
Total Accepted: 31557 Total Submissions: 116793 Given a triangle, find the minimum path sum from ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- 【LeetCode OJ】Interleaving String
Problem Link: http://oj.leetcode.com/problems/interleaving-string/ Given s1, s2, s3, find whether s3 ...
- 【LeetCode OJ】Reverse Words in a String
Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...
- LeetCode OJ学习
一直没有系统地学习过算法,不过算法确实是需要系统学习的.大二上学期,在导师的建议下开始学习数据结构,零零散散的一学期,有了链表.栈.队列.树.图等的概念.又看了下那几个经典的算法——贪心算法.分治算法 ...
- 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 ...
- 备份LeetCode OJ自己编写的代码
常泡LC的朋友知道LC是不提供代码打包下载的,不像一般的OJ,可是我不备份代码就感觉不舒服- 其实我想说的是- 我自己写了抓取个人提交代码的小工具,放在GitCafe上了- 不知道大家有没有兴趣 ht ...
- 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 ...
随机推荐
- c# HttpClient获取网页源码
#region 获取网页源码 public static string HttpClientGetHtmls(string url) { try { var client = new HttpClie ...
- 剑指offer 面试67题
面试67题: 题目: 链接:https://www.nowcoder.com/questionTerminal/1277c681251b4372bdef344468e4f26e?commentTags ...
- python中的标识符长度能有多长
在python中,标识符可以还是任意长度.此外,我们在命名标识符时还必须遵守以下规则 1 只能以下划线或者A-Z/a-z中字母开头 2 其余部分可以使用A-Z/a-z/0-9 3 区分大小写 4 关键 ...
- __init__和__new__
一.__init__方法是什么 __init__方法通常用在初始化一个类实例的时候, class Person(object): """Silly Person" ...
- SpringBoot整合集成redis
Redis安装:https://www.cnblogs.com/zwcry/p/9505949.html 1.pom.xml <project xmlns="http://maven. ...
- android 获取视频缩略图终极解决方案(ffmpeg)
http://blog.csdn.net/u010499721/article/details/50338623 前些天有个师弟(在做一个仿LinkInEyes行车记录仪的app)问我怎么获取视频缩略 ...
- Python编程-网络编程
一.Socket复习 1.Socket参数 sk.bind(address) 必会 s.bind(address) 将套接字绑定到地址.address地址的格式取决于地址族.在AF_INET下,以元组 ...
- python日志操作logging
步骤: 1.定义一个日志收集器 my_logger = logging.getLogger("kitty") 2.设定级别.默认为warning:debug,,info,error ...
- nodejs安装,配置环境,使用express建立一个新项目
1.下载nodejs安装包 去nodejs官网下载最新版本就行,网址:http://nodejs.cn/download/,点击自己适用的系统,自动下载跟电脑操作系统位数符合的安装包, 下载下来安装包 ...
- perl FAQ(zz)
1. Why do you write a program in Perl? Ans : Easy to use and fast execution since perl script underg ...