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 <= 0)
            return 0;
        if(height == 1)
            return triangle[0][0];
        int width = triangle[height-1].size();
        vector<vector<int>> result(height,vector<int>(width));
        result[0][0] = triangle[0][0];
        for(int i = 1;i < height;i++){
            width = triangle[i].size();
            for(int j = 0;j < width;j++){
                if(j !=0 && j != (width-1)){
                    result[i][j] = min(result[i-1][j] + triangle[i][j],result[i-1][j-1] + triangle[i][j]);
                }
                else if(j == 0)
                    result[i][j] = result[i-1][j] + triangle[i][j];
                else
                    result[i][j] = result[i-1][j-1] + triangle[i][j];
            }
        }
        int min_num = 0x7fffffff;
        for(int i = 0;i < triangle[height-1].size();i++){
            if(min_num > result[height-1][i])
                min_num = result[height-1][i];
        }
        return min_num;
    }
};
时间: 2024-08-28 02:27:52

120. Triangle 以及一个多维vector如何初始化的相关文章

二维vector的初始化

1 #include <iostream> 2 #include <vector> 3 using namespace std; 4 5 int main() 6 { 7 int m, n; 8 cin >> m >> n; 9 vector<vector<int> > value(m, vector<int>(n)); // 两个>用空格分开 10 for (int i = 0; i < m; i++){ 1

结对开发之返回一个二维整数数组中最大联通子数组的和

一.题目要求 输入一个二维整形数组,数组里有正数也有负数.二维数组首尾相接,象个一条首尾相接带子一样.数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和.求所有子数组的和的最大值.要求时间复杂度为O(n)题目:返回一个二维整数数组中最大子数组的和 二.解题思路 先对二维数组进行了重构,形成一个环状二维数组,然后再用求二维数组子矩阵最大和的方法求得最终结果. 三.程序代码 2 #include<iostream.h> 3 int main(int argc, char* argv[]

动态创建二维vector数组+指针与引用的区别

二维vectorvector<vector <int> > ivec(m ,vector<int>(n));    //m*n的二维vector 动态创建m*n的二维vector方法一:vector<vector <int> > ivec;ivec.resize(m);for(int i=0;i<m;i++) ivec[i].resize(n); 方法二:vector<vector <int> > ivec;ivec

[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 following properties: Integers in each row are sorted in ascending from left to right. Integers in each column are sorted in ascending from top to bottom.

[LeetCode] Search a 2D Matrix 搜索一个二维矩阵

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previous ro

[LeetCode] 240. Search a 2D Matrix II 搜索一个二维矩阵 II

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted in ascending from left to right. Integers in each column are sorted in ascending from top to bottom.

从无到有写一个运维APP(三)完结篇

前言:自己的挖的坑还得填,此篇为完结篇,环境的搭建参考第一篇从无到有写一个运维APP(一),至于第二篇就跳过吧,写个APP没那么复杂.由于自己现在无业游民,所以没有什么现成的环境,环境就随便找个公网的..再者当下的完成度应该算不上一个完整的APP,但是作为参考,依瓢画葫芦绝对足够了,如果等完整产品,可能得等一段时间了,下面的是该项目的地址. 项目地址: https://github.com/youerning/MyApp(star一下呗) 效果图如下 文章目录: 准备工作 代理 页面框架 获取数

java 数据结构 图中使用的一些常用算法 图的存储结构 邻接矩阵:图的邻接矩阵存储方式是用两个数组来标示图。一个一位数组存储图顶点的信息,一个二维数组(称为邻接矩阵)存储图中边或者弧的信息。 设图G有n个顶点,则邻接矩阵是一个n*n的方阵,定义为: 实例如下,左图是一个无向图。右图是邻接矩阵表示:

以下内容主要来自大话数据结构之中,部分内容参考互联网中其他前辈的博客. 图的定义 图是由顶点的有穷非空集合和顶点之间边的集合组成,通过表示为G(V,E),其中,G标示一个图,V是图G中顶点的集合,E是图G中边的集合. 无边图:若顶点Vi到Vj之间的边没有方向,则称这条边为无项边(Edge),用序偶对(Vi,Vj)标示. 对于下图无向图G1来说,G1=(V1, {E1}),其中顶点集合V1={A,B,C,D}:边集合E1={(A,B),(B,C),(C,D),(D,A),(A,C)}: 有向图:若

基于zabbix用Python写一个运维流量气象图

前言:同事问我,你写运维平台最先写哪一部分?好吧,还真把我问倒了,因为这是在问最应该放在放在第一位的东西~作为一个工作不足两年,运维不足一年的新手来说,还真不敢妄下评论,其实按照我的思路,觉得最重要的部分肯定是故障处理,报警,但是这一块怎么写?怎么说?肯定不能重复造轮子了,不过我最想写的是报表系统,思路是有的,但是一直耽搁了,详情参考http://youerning.blog.51cto.com/10513771/1708925. 好吧,在回到那个问题,应该先写哪个部分.我没回答,反问他了. 他