class Solution {
public:
int minimumTotal(vector<vector<int>>& triangle) {
if(triangle.size() == ) return triangle[][];
triangle[][] += triangle[][];triangle[][] += triangle[][];
if(triangle.size() == ) return min(triangle[][],triangle[][]);
int a = min(triangle[][],triangle[][]);
for(int i=;i < ;i++){
triangle[][i] += a;
}
triangle[][] += triangle[][];
for(int i=;i < triangle.size();i++){
triangle[i][] += min(triangle[i-][],triangle[i-][]);
for(int j=;j < triangle[i].size()-;j++){
int x = min(triangle[i-][j-],triangle[i-][j]);
int y = min(x,triangle[i-][j+]);
triangle[i][j] += y;
}
triangle[i][triangle[i].size()-] += min(triangle[i-][triangle[i].size()-],triangle[i-][triangle[i].size()-]);
triangle[i][triangle[i].size()-] += triangle[i-][triangle[i].size()-];
}
return findmin(triangle[triangle.size()-]);
}
int findmin(vector<int> temp){
int minnum = temp[];
for(int i=;i < temp.size();i++){
if(minnum > temp[i]) minnum = temp[i];
}
return minnum;
}
};

——题意没写清楚,相邻是左中右还是中右,WA了一发

class Solution {
public:
int minimumTotal(vector<vector<int>>& triangle) {
if(triangle.size() == ) return triangle[][];
triangle[][] += triangle[][];triangle[][] += triangle[][];
for(int i=;i < triangle.size();i++){
triangle[i][] += triangle[i-][];
for(int j=;j < triangle[i].size()-;j++){
triangle[i][j] += min(triangle[i-][j-],triangle[i-][j]);
}
triangle[i][triangle[i].size()-] += triangle[i-][triangle[i].size()-];
}
return findmin(triangle[triangle.size()-]);
}
int findmin(vector<int> temp){
int minnum = temp[];
for(int i=;i < temp.size();i++){
if(minnum > temp[i]) minnum = temp[i];
}
return minnum;
}
};

_

Leetcode 120的更多相关文章

  1. [LeetCode 120] - 三角形(Triangle)

    问题 给出一个三角形,找出从顶部至底部的最小路径和.每一步你只能移动到下一行的邻接数字. 例如,给出如下三角形: [ [2], [3,4], [6,5,7], [4,1,8,3] ] 从顶部至底部的最 ...

  2. LeetCode 120. Triangle (三角形)

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  3. 算法练习 —— LeetCode 1-20题

    一.两数之和 1.1 题目描述 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, ...

  4. leetcode 1-20 easy

    1.Two Sum Given an array of integers, return indices of the two numbers such that they add up to a s ...

  5. Java实现 LeetCode 120 三角形最小路径和

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

  6. LeetCode - 120. Triangle

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  7. leetcode 120 Triangle ----- java

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  8. [LeetCode] 120. Triangle _Medium tag: Dynamic Programming

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  9. [leetcode 120]triangle 空间O(n)算法

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

随机推荐

  1. 使用MVCPager做AJAX分页所需要注意的地方

    1.版本问题,推荐使用2.0以上,对ajax支持才比较的好了 2.当需要使用页索引输入或下拉框以及使用Ajax分页模式时,必须用Html.RegisterMvcPagerScriptResource方 ...

  2. Ubuntu18.04下安装MySQL

    Ubuntu上安装MySQL非常简单只需要几条命令就可以完成. 1. sudo apt-get install mysql-server 2. apt-get isntall mysql-client ...

  3. JDBC连接数据库的安全性连接方法

    PreparedStatement ps=null; ResultSet rs=null; Connection ct=null; try { Class.forName("com.mysq ...

  4. hadoop中 bin/hadoop fs -ls ls: `.': No such file or directory问题

    2.x版本上的使用bin/hadoop fs -ls  /就有用 应该使用绝对路径就不会有问题 mkdir也是一样的 原因:-ls默认目录是在hdfs文件系统的/user/用户名(用户名就命令行@符号 ...

  5. 首篇 sdk 之 AlertDialog

    带着十足的干劲,用着有限的英语水平,我们来看看sdk里docs里的AlertDialog: AlertDialog SDK 原文描述:A dialog that can show a title, u ...

  6. python静态方法、类方法

    常规: class Dog(object): def __init__(self,name): self.name=name def eat(self): print('%s is eating'%s ...

  7. leecode第七十八题(子集)

    class Solution { public: vector<vector<int>> subsets(vector<int>& nums) { vect ...

  8. python类中保存非绑定方法作为成员函数

    习惯了函数式,动不动传一个函数.但是直接把函数作为类方法保存,再调用时会报错. 举一个unittest时的例子 class MyTestCase(unittest.TestCase): @classm ...

  9. 一个sql实现查询并且插入到另一个表中

    两种不同方法,结果不同 方法一.查询的user表中3个元素,name为user表中的字段,1000,0,是往department中要赋的值(给id赋值) ,`name`,' 方法二(推荐使用方法二): ...

  10. h5 audio进度条

    h5 audio 播放进度条 效果图: html部分: <div class="audiojindu"> <div class="playcontrol ...