120. Triangle 以及一个多维vector如何初始化
1.刚开始result的初始化写的是vector<vector<int>> result,然后再去对result[0][0] = triangle[0][0]赋值,一直报错。老问题了!
2.多维vector的初始化:
vector<vector<int>> result(height,vector<int>(width))
class Solution {
public:
int minimumTotal(vector<vector<int>>& triangle) {
int height = triangle.size();
if(height <= )
return ;
if(height == )
return triangle[][];
int width = triangle[height-].size();
vector<vector<int>> result(height,vector<int>(width));
result[][] = triangle[][];
for(int i = ;i < height;i++){
width = triangle[i].size();
for(int j = ;j < width;j++){
if(j != && j != (width-)){
result[i][j] = min(result[i-][j] + triangle[i][j],result[i-][j-] + triangle[i][j]);
}
else if(j == )
result[i][j] = result[i-][j] + triangle[i][j];
else
result[i][j] = result[i-][j-] + triangle[i][j];
}
}
int min_num = 0x7fffffff;
for(int i = ;i < triangle[height-].size();i++){
if(min_num > result[height-][i])
min_num = result[height-][i];
}
return min_num;
}
};
简化版
class Solution {
public:
int minimumTotal(vector<vector<int>>& triangle) {
int length = triangle.size();
vector<vector<int>> result(length,vector<int>(length));
result[][] = triangle[][];
for(int i = ;i < length;i++){
for(int j = ;j <= i;j++){
if(j == )
result[i][j] = result[i-][j] + triangle[i][j];
else if(j == i)
result[i][j] = result[i-][j-] + triangle[i][j];
else
result[i][j] = min(result[i-][j],result[i-][j-]) + triangle[i][j];
}
}
int min_num = 0x7FFFFFFF;
for(int i = ;i < length;i++){
if(result[length-][i] < min_num)
min_num = result[length-][i];
}
return min_num;
}
};
120. Triangle 以及一个多维vector如何初始化的更多相关文章
- C++STL中vector的初始化
vector的初始化有很多方式,在N维初始化时还会一些容易出现错误的地方.下面进行总结 以下的总结均以int作为模板参数 一维vector的初始化 vector的构造函数通常来说有五种,如下: vec ...
- LeetCode 120. Triangle三角形最小路径和 (C++)
题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...
- leetcode 118. Pascal's Triangle 、119. Pascal's Triangle II 、120. Triangle
118. Pascal's Triangle 第一种解法:比较麻烦 https://leetcode.com/problems/pascals-triangle/discuss/166279/cpp- ...
- LeetCode 120. Triangle (三角形最小路径和)详解
题目详情 给定一个三角形,找出自顶向下的最小路径和.每一步只能移动到下一行中相邻的结点上. 例如,给定三角形: [ [2], [3,4], [6,5,7], [4,1,8,3] ] 自顶向下的最小路径 ...
- [LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [LeetCode] Search a 2D Matrix 搜索一个二维矩阵
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [CareerCup] 11.6 Search a 2D Matrix 搜索一个二维矩阵
11.6 Given an M x N matrix in which each row and each column is sorted in ascending order, write a m ...
- 动态创建二维vector数组 C和C++ 及指针与引用的区别
二维vectorvector<vector <int> > ivec(m ,vector<int>(n)); //m*n的二维vector 动态创建m*n的二 ...
- c++中用vector创建多维数组的初始化方法
最近调试一个程序,在使用vector声明一个二维数组时出现错误.错误的方法如下所示: std::vector<std::vector<double> > sphereGrid; ...
随机推荐
- C# web 总结
(1)Cshtml 中 “@” 符号转义 在 cshtml 中需要使用 “@” 符号,如 “@幸福摩天轮版权所有”.那么我们需要使用转义,使用 “@@” 就好!“© ”和 “@” 好像呀. <t ...
- Jmeter + Junit
在使用jmeter中Junit的功能之前,最好将接口以手动的方式调通,可以参考 Jmeter之HTTP Request 下面开始讲一下如何使用Junit功能 1. 编写测试类 JmeterJunit ...
- 重温sql 设计的基本三大范式
第一范式:确保每列的原子性. 如果每列(或者每个属性)都是不可再分的最小数据单元(也称为最小的原子单元),则满足第一范式. 例如:顾客表(姓名.编号.地址.……)其中"地址"列还可 ...
- 使用Spring Security控制会话
1.概述 在本文中,我们将说明Spring Security如何允许我们控制HTTP会话.此控件的范围从会话超时到启用并发会话和其他高级安全配置. 2.会话何时创建? 我们可以准确控制会话何时创建以及 ...
- lightoj1087 【线段树】
题意: 给你n个数,然后给你q个询问,有两种询问: a: 表示在右边插入一个数 c:表示从左边拿出一个数,然后输出: 思路: 一开始在想,自己手上的黑科技:线段树和树状数组 线段树上的操作: 求区间最 ...
- Type中的3个bool属性: IsGenericType , IsGenericTypeDefinition , IsGenericParameter
首先说下 IsGenericType 用3个实例说明: typeof(DateTime).IsGenericType : false typeof(List<int>).IsGeneric ...
- Xmind8 Pro 思维导图制作软件,傻瓜式安装激活教程
xmind 是做思维导图的软件?今天有一个以前的同事还在和我要这个软件,当然我支持正版啊 !因为正版好用! 我是一个不爱说废话的人,就顺便分享一下 给大家用! 软件下载地址: 链接:https://p ...
- RobotFramework特性总结
robotframework是一款python编写的功能自动化测试框架.具备良好的可扩展性,支持关键字驱动,可以同时编写多种类型的客户端或者接口,可以进行分布式测试执行.主要用户轮次很多的验收测试和验 ...
- C 语言实例 - 连接字符串
C 语言实例 - 连接字符串 C 语言实例 C 语言实例 使用 strcat() 连接两个字符串. 实例 #include <stdio.h> int main() { ], s2[], ...
- get 和 post 请求的区别(转)
转自 http://www.cnblogs.com/hyddd/archive/2009/03/31/1426026.html http://www.nowamagic.net/librarys/ve ...