C++

逆推

 class Solution {
public:
/**
* @param triangle: a list of lists of integers.
* @return: An integer, minimum path sum.
*/
int minimumTotal(vector<vector<int> > &triangle) {
// write your code here
if (triangle.size() == )
return ; vector<int> dp(triangle.size()); dp[] = triangle[][];
for(int i = ; i < triangle.size(); i++)
for(int j = triangle[i].size() - ; j >= ; j--)
if (j == )
dp[j] = dp[j] + triangle[i][j];
else if (j == triangle[i].size() - )
dp[j] = dp[j-] + triangle[i][j];
else
dp[j] = min(dp[j-], dp[j]) + triangle[i][j]; int ret = INT_MAX;
for(int i = ; i < dp.size(); i++)
ret = min(ret, dp[i]); return ret;
}
};

C++,修改原数组

 class Solution {
public:
/**
* @param triangle: a list of lists of integers.
* @return: An integer, minimum path sum.
*/
int minimumTotal(vector<vector<int> > &triangle) {
// write your code here
for (int i = triangle.size() - ; i >= ; --i){
for (int j = ; j < i + ; ++j){
if(triangle[i+][j] > triangle[i+][j+]){
triangle[i][j] += triangle[i+][j+];
}else{
triangle[i][j] += triangle[i+][j];
}
}
}
return triangle[][];
}
};

LintCode: Triangle的更多相关文章

  1. LintCode "Triangle Count"

    Should be "Medium" or even "Easy".. Just with a little Greedy. class Solution { ...

  2. 【Lintcode】382.Triangle Count

    题目: Given an array of integers, how many three numbers can be found in the array, so that we can bui ...

  3. leetcode 611. Valid Triangle Number 、259. 3Sum Smaller(lintcode 918. 3Sum Smaller)

    这两个题几乎一样,只是说611. Valid Triangle Number满足大于条件,259. 3Sum Smaller满足小于条件,两者都是先排序,然后用双指针的方式. 611. Valid T ...

  4. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  5. (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)

    --------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...

  6. leetcode & lintcode for bug-free

    刷题备忘录,for bug-free leetcode 396. Rotate Function 题意: Given an array of integers A and let n to be it ...

  7. leetcode & lintcode 题解

    刷题备忘录,for bug-free 招行面试题--求无序数组最长连续序列的长度,这里连续指的是值连续--间隔为1,并不是数值的位置连续 问题: 给出一个未排序的整数数组,找出最长的连续元素序列的长度 ...

  8. lintcode 刷题 by python 总结(1)

    博主之前在学习 python 的数据结构与算法的基础知识,用的是<problem-solving-with-algorithms-and-data-structure-using-python& ...

  9. lintcode算法周竞赛

    ------------------------------------------------------------第七周:Follow up question 1,寻找峰值 寻找峰值 描述 笔记 ...

随机推荐

  1. SharePoint Online 创建网站集

    前言 本文介绍如何在Office 365中创建SharePoint网站集. 正文 通过登录地址登录到Office 365环境中,我们可以在左上角的按钮中点开,进入管理员,也可以直接在页面中点击管理: ...

  2. 较老版本 AFNetworking 使用心得

    较老版本的 AFNetworking 下载链接 http://pan.baidu.com/s/14Cxga 将压缩包中的文件夹拖入xcode工程项目中并引入如下的框架 简单的 JOSN 解析例子    ...

  3. Android之Android apk动态加载机制的研究

    转载请注明出处:http://blog.csdn.net/singwhatiwanna/article/details/22597587 (来自singwhatiwanna的csdn博客) 背景 问题 ...

  4. byte[],bitmap,drawable之间的相互转换

    Byte[]转Bitmap BitmapFactory.decodeByteArray(data, 0, data.length); Bitmap转Byte[] ByteArrayOutputStre ...

  5. springMVC4(14)各类视图输出实例分析

    1. 模板视图 FreeMarkerViewResolver . VolocityViewResolver 这两个视图解析器都是 UrlBasedViewResolver 的子类. FreeMarke ...

  6. 奇怪吸引子---ChenLee

    奇怪吸引子是混沌学的重要组成理论,用于演化过程的终极状态,具有如下特征:终极性.稳定性.吸引性.吸引子是一个数学概念,描写运动的收敛类型.它是指这样的一个集合,当时间趋于无穷大时,在任何一个有界集上出 ...

  7. 【转】Redis 总结精讲 看一篇成高手系统-4

    https://www.cnblogs.com/rjzheng/p/9096228.html 本文围绕以下几点进行阐述 1.为什么使用redis2.使用redis有什么缺点3.单线程的redis为什么 ...

  8. Html.AntiForgeryToken() 防止CSRF攻击 的AJaX应用

    有关Html.AntiForgeryToken()的使用其实网上的说明很多了,比如http://blog.csdn.net/cpytiger/article/details/8781457 那么我们写 ...

  9. 网页重构应该避免的10大 CSS 糟糕用法

    对于网页重构来说,CSS禅意花园 是网页布局从 table 表格转到了 html +css 的标志 .这些年来,随着我们的网站越来越复杂:html5,css3,新的技术.新的属性,越来越多的开发者开始 ...

  10. \r\n 如何转换成utf-8格式的,在jsp页面中正常显示换行

    数据库中存的换行是\r\n,读取到UTF-8编码的JSP 页面上以后,没有换行 在jsp页面显示之前,要进行一下字符转换,将所有\r\n转换成<br>,如下: String result  ...