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, ...
随机推荐
- linux发行版及版本号
1991年8月:Linus Torvalds宣布成立Linux 遵行GPL: Kernel:底层监控程序又叫通用程序,即我们所说的操作系统 Kernel的作用: ...
- 高性能mysql 第11章 可扩展的mysql
可扩展性的定义:当增加资源以获得执行更多的工作系统能获得划算的同等提升. 向上扩展(垂直扩展):提升服务器的硬件性能. 向外扩展(水平扩展):一般都是复制,拆分,数据分片(sharding). 复制: ...
- 入门Leaflet之小Demo
入门Leaflet之小Demo 写在前面 ---- WebGIS开发基础之Leaflet GIS基本概念:GIS.Map.Layer.Feature.Geometry.Symbol.Data(Poin ...
- Web前端经典面试试题(一)
本篇收录了一些面试中经常会遇到的经典面试题,并且都给出了我在网上收集的答案.眼看新的一年马上就要开始了,相信很多的前端开发者会有一些跳槽的悸动,通过对本篇知识的整理以及经验的总结,希望能帮到更多的前端 ...
- electron限制只启动一个应用
electron限制只启动一个应用 // ========================================================== // 限制只可以打开一个应用,2.x的文 ...
- CSS 的伪元素是什么?
CSS伪元素是用来添加一些选择器的特殊效果.用于:向某个选择器中的文字的首行. ㈠语法 ①伪元素的语法: selector:pseudo-element {property:value;} ②CSS类 ...
- hdu 2604 Queuing(推推推公式+矩阵快速幂)
Description Queues and Priority Queues are data structures which are known to most computer scientis ...
- linux 免密码 使用sudo 直接使用root权限执行命令
1.切换到root用户下,怎么切换就不用说了吧,不会的自己百度去. 2.添加sudo文件的写权限,命令是: chmod u+w /etc/sudoers 3.编辑sudoers文件 vi /etc/s ...
- CF1043F Make It One 容斥+dp+组合
考试的时候考的一道题,感觉挺神的. 我们发现将所有数去重后最多只会选不到 $7$ 后 $gcd$ 就会变成 $1$. 令 $f[i][k]$ 表示选 $i$ 个数后 $gcd$ 为 $k$ 的方案数. ...
- css grid 随笔
原文出自Arien的博客https://www.w3cplus.com/css3/line-base-placement-layout.html 首先定义一个网格 1.可以给父容器的display属性 ...