118. Pascal's Triangle

第一种解法:比较麻烦

https://leetcode.com/problems/pascals-triangle/discuss/166279/cpp-beats-1002018.9.3(with-annotation)

class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> result;
vector<int> res;
for(int i = ;i <= numRows;i++){
for(int j = ;j <= i;j++)
res.push_back();
result.push_back(res);
res.clear();
}
if(numRows <= )
return result;
for(int i = ;i < numRows;i++){
for(int j = ;j < i;j++){
result[i][j] = result[i-][j-] + result[i-][j];
}
}
return result;
}
};

第二种解法:

http://www.cnblogs.com/grandyang/p/4032449.html

class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> result(numRows,vector<int>(numRows,));
for(int i = ;i < numRows;i++){
result[i].resize(i+);
for(int j = ;j <= i-;j++){
result[i][j] = result[i-][j-] + result[i-][j];
}
}
return result;
}
};

119. Pascal's Triangle II

这个题与118. Pascal's Triangle不同的是只求这一行的数值,并且输入的是下标index,不是n,即index = n-1。

这个题要实现O(k) 的空间复杂度,那申请存储的大小就只能是k个大小,即那行所具有的元素的个数。

每个数值来自于上一行同列的数值+上一行小一列的数值。

当前这个一维数组存储的值,其实是上一行同列的值,所以只需要再加上小一列的数值即可以。

注意:这里只能从每行的右侧向左侧进行计算,不能从左侧向右侧进行计算,只有从右侧向左侧计算,当前位置存储的原始值才能代表上一行同列的值。

从右向左的原因是当前位置的值是上一行当前位置和上一行前一个位置的值的和,所以前一个要在后一个位置之后发生变化

类似如下图:

实质上就是每次增加一行,增加了一列,所有0之前的数字都对应保存着上一行同样列的值

class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> res(rowIndex + ,);
res[] = ;
for(int i = ;i <= rowIndex;i++){
for(int j = i;j > ;j--){
res[j] += res[j-];
}
}
return res;
}
};

120. Triangle

https://www.cnblogs.com/grandyang/p/4286274.html

暴力的方式是将所有路径搜索一遍,这样的时间复杂度是n!。

方法一是使用动态规划的方法,当前值只可能来自同行同列和同行少一列。

方法一:

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;
}
};

方法二:

此方法将空间复杂度优化到O(n)。类似Pascal's Triangle II用一个一维数组进行计算,但此题是从最后一行出发进行遍历。

class Solution {
public:
int minimumTotal(vector<vector<int>>& triangle) {
int m = triangle.size();
if(m <= )
return ;
vector<int> dp = triangle[m-];
for(int i = m-;i >=;i--){
for(int j = ;j <= i;j++){
dp[j] = min(dp[j],dp[j+]) + triangle[i][j];
}
}
return dp[];
}
};

leetcode 118. Pascal's Triangle 、119. Pascal's Triangle II 、120. Triangle的更多相关文章

  1. [LeetCode]题解(python):119 Pascal's Triangle II

    题目来源 https://leetcode.com/problems/pascals-triangle-ii/ Given an index k, return the kth row of the ...

  2. 【LeetCode】118 & 119 - Pascal's Triangle & Pascal's Triangle II

    118 - Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, ...

  3. 【LEETCODE】34、119题,Pascal's Triangle II

    package y2019.Algorithm.array; import java.util.ArrayList; import java.util.List; /** * @ProjectName ...

  4. LeetCode 118 Pascal's Triangle

    Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows  ...

  5. 118/119. Pascal's Triangle/II

    原文题目: 118. Pascal's Triangle 119. Pascal's Triangle II 读题: 杨辉三角问题 '''118''' class Solution(object): ...

  6. [LeetCode] 119. Pascal's Triangle II 杨辉三角 II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

  7. LeetCode OJ 119. Pascal's Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

  8. LeetCode—66、88、118、119、121 Array(Easy)

    66. Plus One Given a non-negative integer represented as a non-empty array of digits, plus one to th ...

  9. leetcode 118

    118. Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, ...

随机推荐

  1. electron builder 打包多个第三方依赖的软件

    背景 在实际的开发过程中,我们最后打包生成的exe.会依赖一些第三方的软件,或者说是一些系统的环境,比如 .net framework vc++ 等,这些环境不能依赖客户的环境,所以最好的做法是在打包 ...

  2. cassandra查询效率探讨

    cassandra目前提倡的建表与查询方式为CQL方式,传统的cassandra-cli相关的api由于性能问题将逐步淘汰,而cassandra-cli也将在2.2版本之后被淘汰. 在CQL中,可以利 ...

  3. Clob类型转换为String

    SQL CLOB 是内置类型,它将字符大对象存储为数据库表某一行中的一个列值,使用CHAR来存储数据,如XML文档. 如下是一个Clob转换为String的静态方法,可将其放在自己常用的工具类中,想直 ...

  4. web规范文档说明三

    网站头部:    head/header(头部) top(顶部)导航:   nanv 导航具体区分:topnav(顶部导航).mainnav(主导航).mininav(迷你导航).textnav(导航 ...

  5. 详解thinkphp+redis+队列的实现代码

    1,安装Redis,根据自己的PHP版本安装对应的redis扩展(此步骤简单的描述一下) 1.1,安装 php_igbinary.dll,php_redis.dll扩展此处需要注意你的php版本如图: ...

  6. 使用rpm安装mysql5.6(简单安装 实验使用)

    [root@localhost mysql]# cd /usr [root@localhost mysql]# mkdir mysql [root@localhost mysql]# cd mysql ...

  7. 各种环境下搭建ruby on rails开发环境

    win10上搭建raby on rails环境: 步骤如下 1.安装ruby (我选择的版本是ruby 2.2.3p173) 2.安装rails gem 在这之前建议先把gem的源换成淘宝的源,速度快 ...

  8. Python 3标准库 第十一章 网路通信

    网路通信11.1 ipaddress  : Internet 地址 ipaddress模块提供了处理IPv4和IPv6 11.1.2  网络 CMDB  11.2  socket:网络通信 11.2. ...

  9. 在angular中使用ng-repeat时数组中有重复元素,要用item in items track by $index

    在angular中使用ng-repeat时数组中有重复元素,要用item in items track by $index,不然会报错 <div class="" ng-in ...

  10. Linux培训教程lgzip命令详解和使用实例

    gzip不仅可以用来压缩大的.较少使用的文件以节省磁盘空间,还可以和tar命令一起构成Linux操作系统中比较流行的压缩文件格式.据统计,gzip命令对文本文件有60%-70%的压缩率. 1.命令格式 ...