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-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的更多相关文章
- [LeetCode]题解(python):119 Pascal's Triangle II
题目来源 https://leetcode.com/problems/pascals-triangle-ii/ Given an index k, return the kth row of the ...
- 【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, ...
- 【LEETCODE】34、119题,Pascal's Triangle II
package y2019.Algorithm.array; import java.util.ArrayList; import java.util.List; /** * @ProjectName ...
- LeetCode 118 Pascal's Triangle
Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...
- 118/119. Pascal's Triangle/II
原文题目: 118. Pascal's Triangle 119. Pascal's Triangle II 读题: 杨辉三角问题 '''118''' class Solution(object): ...
- [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, ...
- 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, ...
- 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 ...
- leetcode 118
118. Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, ...
随机推荐
- 网络初级篇之OSPF(二)实验
一.实验目的: 下面关于OSPF的实验,仔细看配置过程,以增加对OSPF的理解. 二.实现目标: 使用OSPF实现所有主机之间的通信 三.配置过程: 1.AR1的配置过程: ...
- 使用NPOI或POI 导出Excel大数据(百万级以上),导致内存溢出的解决方案(NPOI,POI)
使用工具:POI(JAVA),NPOI(.Net) 致谢博主 Crazy_Jeff 提供的思路 一.问题描述: 导出任务数据量近100W甚至更多,导出的项目就会内存溢出,挂掉. 二.原因分析: 1.每 ...
- 【转】Java的四种代码块
原贴地址:http://www.cnblogs.com/end/archive/2012/12/21/2827554.html 一.普通代码块 直接在一个方法中出现的{}就称为普通代码块,例子程序如下 ...
- [uboot] (番外篇)global_data介绍(转)
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/ooonebook/article/det ...
- java8 stream 注意点
https://blog.csdn.net/lixiaobuaa/article/details/81099838 首先,Stream流有一些特性: Stream流不是一种数据结构,不保存数据,它只是 ...
- 多线程(三) 同步synchronized
五.同步 1.锁 多线程程序一般是为了完成一些相同的工作而存在的,因此有时间也会共享一些资源,例如对象.变量等等,此时如果不对各个线程进行资源协调,就会出现一些冲突,从而导致程序功能失效.例如下面的示 ...
- MyEclipse开发秘籍——使用DevStyle Icon Designer(一)
[MyEclipse CI 2019.4.0安装包下载] DevStyle主题包括一个Icon Designer,允许您自定义图标.Icon Designer包含在DevStyle主题中,可作为Ecl ...
- sublime添加书签
ctrl+f2添加书签, f2切换书签
- Centos7——docker持久化存储和卷间状态共享(笔记)
docker持久化存储和卷间状态共享(笔记) 本章介绍 存储卷的介绍 存储卷的两种类型 宿主机好额容器之间如何共享数据 容器之间如何共享数据 存储卷的声明周期 存储卷之间的数据管理和控制模式 就像在 ...
- 【leetcode】1253. Reconstruct a 2-Row Binary Matrix
题目如下: Given the following details of a matrix with n columns and 2 rows : The matrix is a binary mat ...