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; ...
随机推荐
- 《剑指offer》面试题1:为类CMyString添加赋值运算符函数——C++拷贝构造函数与赋值函数
题中已给出CMyString的类定义,要求写赋值运算符函数. #include<iostream> #include<cstring> using namespace std; ...
- 一个C#开发者重温C++的心路历程
不知道为什么,似乎很多人理解跑偏了,在这里我要说明一下. 首先,我并没有对C++语言有偏见,我只是单纯的在学习时,在理解时,对C++语言进行一些吐槽,我相信,很多学习C++的人,也会有类似的吐槽. 其 ...
- 51nod 1099【贪心】
思路: 我们可以思考对于仅仅两个元素来说,A,B; 先选A的话是会 A.b+B.a; 先选B的话是会 B.b+A.a; 所以哪个小哪个就放前面; #include <cstdio> #i ...
- Java内存模型(Java Memory Model,JMM)
今天简单聊聊什么叫做 Java 内存模型,不是 JVM 内存结构哦. JMM 是一个语言级别的内存模型,处理器的硬件模型是硬件级别,Java中的内存模型是内存可见性的基本保证.从而为我们 volati ...
- 阿里云物联网 .NET Core 客户端 | CZGL.AliIoTClient:3. 订阅Topic与响应Topic
文档目录: 说明 1. 连接阿里云物联网 2. IoT 客户端 3. 订阅Topic与响应Topic 4. 设备上报属性 4.1 上报位置信息 5. 设置设备属性 6. 设备事件上报 7. 服务调用 ...
- python+unittest 搭建简易的接口测试框架
主要介绍如何使用python+unittest快速搭建一个接口测试的框架 1.安装python unittest 2.新建一个python项目ApiTest 在setUp和setDown里设置一些需 ...
- github网页样式
- Tinghua Data Mining 9
关联规则,营销购物 空缺 协同过滤
- MyBatis中时间格式的映射问题
简单地说,就是Java的Date类可以直接映射到Mysql的TIMESTAMP或者是DATETIME(按道理应该是映射成DATE的) 具体的看这两篇博客吧: 1. MySql中TIMESTAMP和DA ...
- 101 Symmetric Tree 判断一颗二叉树是否是镜像二叉树
给定一个二叉树,检查它是否是它自己的镜像(即,围绕它的中心对称).例如,这个二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \3 4 4 3但是 ...