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. spring配置freemarker

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  2. 【Ruby】【改gem源镜像】【Win10 + Jruby-9.1.2.0 + Rails 5.1.3 + gem 2.6.4 】

    参考地址:https://ruby-china.org/topics/33843 (1)> gem sources --add http://gems.ruby-china.org 遇到问题: ...

  3. RN 实现简易浏览器

    import React, { Component } from 'react'; import { AppRegistry, StyleSheet, Text, View, Dimensions, ...

  4. Antd-Select组件的深入用法

    一.Antd-Select提供几种类型 最基础版只提供下拉功能的选择器 带搜索功能的下拉选择器 可多选的下拉选择器 可搜索.可多选.可随意输入内容的tag下拉选择器(支持自动分词) 多级联动下拉选择器 ...

  5. linux服务器安装brook服务端 使用brook客户端

    既然你已经找到了此文章,说明已经知道brook的用途了,不做介绍,下面讲安装方法: 连接服务器,随便cd一个安装目录,例如: mkdir brook && cd brook 2.进re ...

  6. Oracle 11G Client客户端安装

    参考资料: http://www.cnblogs.com/jiguixin/archive/2011/09/09/2172672.html http://blog.csdn.net/lanchengx ...

  7. input标签让光标不出现

    <input  class="red" readonly unselectable="on" > input点击变为搜索框,用form包住绑定搜索事 ...

  8. CSS3实现鼠标移动到图片上图片变大(缓慢变大,有过渡效果,放大的过程是有动画过渡的,这个过渡的时间可以自定义)

    转载自:http://blog.csdn.net/u014175572/article/details/51535768 CSS3的transform:scale()可以实现按比例放大或者缩小功能. ...

  9. Cannot set property 'onclick' of null的问题

    转载自: https://my.oschina.net/ximidao/blog/351017 摘要: 测试点击事件的时候浏览器报错,提示Uncaught TypeError: Cannot set ...

  10. python 部分函数

    abs(number) ,返回数字的绝对值cmath.sqrt(number) ,返回平方根,也可以应用于负数float(object) ,把字符串和数字转换为浮点数help() ,提供交互式帮助in ...