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. 02:Java基础语法(一)

    Java基础语法 Java的关键字及保留字 关键字(Keyword) 关键字的定义和特点定义:被Java语言赋予了特殊含义的单词特点:关键字中所有字母都为小写注意事项:1)true.false.nul ...

  2. Tomcat各版本及源码包下载

    Tomcat各版本及源码包下载 1.百度 Tomcat 进入官网2.Tomcat 官网地址:http://tomcat.apache.org/3.所有 Tomcat 版本及源码包下载地址:https: ...

  3. hdu4405 概率dp

    飞行棋游戏 问从0结束游戏的投色子次数期望是多少 设dp[i]表示i到n的期望,那么可以得到dp[i]=(dp[i+1]+dp[i+2]+dp[i+3]+dp[i+4]+dp[i+5]+dp[i+6] ...

  4. 谈谈对MVC的认识?

    核心思想是:视图和用户交互通过事件导致控制器改变 控制器改变导致模型改变 或者控制器同时改变两者 模型改变 导致视图改变 或者视图改变 潜在的从模型里面获得参数 来改变自己.他的好处是可以将界面和业务 ...

  5. 介绍知道的http返回的状态码

    100    Continue    继续.客户端应继续其请求 101    Switching Protocols    切换协议.服务器根据客户端的请求切换协议.只能切换到更高级的协议,例如,切换 ...

  6. GCD实战之多个网络请求的并发

    // 创建信号量 dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); // 创建全局并行 dispatch_queue_t q ...

  7. centos7安装es

    #安装java1.8rpm -ivh jdk-8u191-linux-x64.rpm #解压estar -zxvf elasticsearch-6.4.0.tar.gz -C /usr #修改es限制 ...

  8. Flink SQL 如何实现数据流的 Join?

    无论在 OLAP 还是 OLTP 领域,Join 都是业务常会涉及到且优化规则比较复杂的 SQL 语句.对于离线计算而言,经过数据库领域多年的积累,Join 语义以及实现已经十分成熟,然而对于近年来刚 ...

  9. static后期静态绑定

    先说一下__CLASS__,get_class() ,  get_called_class() 区别: __CLASS__获取当前的类名, get_class()与上面一样,都是获取当前的类名, ge ...

  10. 重温IOC,DI的理解

    IOC和DI其实它们是同一个概念的不同角度描述 IOC强调的是程序控制对象(创建销毁),变换成了容器来控制对象(创建销毁) DI:即IoC容器帮对象找相应的依赖对象通过反射注入     从Spring ...