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. 89. Gray Code (Java)

    The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...

  2. Linux内核管理子系统和进程管理子系统

    内核管理子系统职能:1.管理虚拟地址与物理地址的映射 2.物理内存的分配 程序:存放在磁盘上的一系列代码和数据的可执行映像,是一个静止的实体. 进程:是一个执行中的程序,它是动态的实体 进程四要素: ...

  3. linux 网卡配置详情

    1.配置文件/etc/hosts(本地主机ip地址映射,可以有多个别名)./etc/services(端口号与标准服务之间的对应关系)./etc/sysconfig/network(设置主机名,网关, ...

  4. 跟着动画来学习TCP三次握手和四次挥手

    TCP三次握手和四次挥手的问题在面试中是最为常见的考点之一.很多读者都知道三次和四次,但是如果问深入一点,他们往往都无法作出准确回答. 点我查看如何应对面试中的三次握手.四次挥手 本篇尝试使用动画来对 ...

  5. 004-linux下配置rsyslog日志收集服务器案例 rsyslog+loganalyzer日志服务器,无法添加报表模板解决

    centos6系统 client1:192.168.1.33 centos7系统 client2:192.168.1.44 centos7系统 master:192.168.1.55 配置服务端mas ...

  6. RPM包搭建

    打包rpm软件包之spec文件解析 1. 概述 RPM的全称是(Red Hat Package Manager,Red Hat包管理器).RPM是一个开放的软件包管理器,工作在Red Hat.类Lin ...

  7. java8学习之自定义收集器实现

    在上次花了几个篇幅对Collector收集器的javadoc进行了详细的解读,其涉及到的文章有: http://www.cnblogs.com/webor2006/p/8311074.html htt ...

  8. Summer training round2 #1

    A:水 B:求两个三角形之间的位置关系:相交 相离 内含 ①用三个点是否在三角形内外判断    计算MA*MB.MB*MC.MC*MA的大小 若这三个值同号,那么在三角形的内部,异号在外部 #incl ...

  9. jquery前端问题随记

    1.图片名称不变,但要求刷新,web页面不重新加载渲染,在url地址后面加t=时间戳 js脚本 img.src=url+"?t="+Math.random() 如果是jsp页面,要 ...

  10. 第二章 Vue快速入门-- 23 品牌案例-根据关键字实现数组的过滤

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...