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; ...
随机推荐
- python---socket与socketserver
1.socket的方socket.getaddrinfo(host, port, family=0, type=0, proto=0, flags=0) #获取要连接的对端主机地址sk.bind(ad ...
- 获取DataGridView上选中的一行并转换为一个DataRow类型
ataGridViewRow gridrow = dataGridView1.SelectedRows[0]; DataRowView row_view = (DataRowView)gridrow. ...
- (水题)洛谷 - P2439 - 阶梯教室设备利用 - 简单dp
https://www.luogu.org/fe/problem/P2439 很明显时间是一个维度,按照时间顺序决策就行了. dp[i]表示以时间i为结尾所能达到的最长演讲时间. #include & ...
- 萌新java入门笔记
首先声明以下内容只是散乱笔记,如果有误还望大侠指出!不胜感激! 基本数据类型: 大体和C语言类似: boolean truth = true;//逻辑型 //文字型 char c; String st ...
- 51nod1352(exgcd)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1352 题意:中文题诶- 思路:exgcd 显然题目可以描述为: ...
- jzoj5986. 【WC2019模拟2019.1.4】立体几何题 (权值线段树)
传送门 题面 题解 不难看出每个点的大小为行列限制中较小的那一个(因为数据保证有解) 对于行的每个限制,能取到的个数是列里限制大于等于它的数的个数,同理,对于列是行里大于它的个数(这里没有等于,为了避 ...
- [Swift]DJSet
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- hyperledger fabric 1.0.5 分布式部署 (五)
梳理fabric e2e_cli 测试程序的具体步骤 作者在 hyperledger fabric 1.0.5 分布式部署 (一)中给读者们介绍了如何从零开始部署一个测试的 demo 环境,如果细心的 ...
- 百度搜索:有关Baiduspider的10个问题
猫宁!!! 参考链接: http://help.baidu.com/question?prod_id=99&class=476&id=2996 https://ziyuan.baidu ...
- 浅谈Nginx服务器的内部核心架构设计
前言 Nginx 是一个 免费的 , 开源的 , 高性能 的 HTTP 服务器和 反向代理 ,以及 IMAP / POP3代理服务器. Nginx 以其高性能,稳定性,丰富的功能,简单的配置和低资源消 ...