118 - Pascal's Triangle

Given numRows, generate the first numRows of Pascal's triangle.

For example, given numRows = 5,
Return

[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]

Solution:

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

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,3,1].

Note:
Could you optimize your algorithm to use only O(k) extra space?

Solution:

class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> vec(,);
vector<vector<int>> ret;
ret.push_back(vec);
int i=;
while(i<=rowIndex){
vec.clear();
vec.push_back();
for(int j=;j<ret[i-].size();j++){
vec.push_back(ret[i-][j-]+ret[i-][j]);
}
vec.push_back();
ret.push_back(vec);
i++;
}
return vec; //or return ret[rowIndex];
}
};

【LeetCode】118 & 119 - Pascal's Triangle & Pascal's Triangle II的更多相关文章

  1. 【LeetCode】117. Populating Next Right Pointers in Each Node II 解题报告(Python)

    [LeetCode]117. Populating Next Right Pointers in Each Node II 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...

  2. 【leetcode】118. Pascal&#39;s Triangle

    @requires_authorization @author johnsondu @create_time 2015.7.23 19:54 @url [Pascal's Triangle](http ...

  3. 【LeetCode】118. Pascal's Triangle 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...

  4. 【LeetCode】118. Pascal's Triangle

    题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,R ...

  5. 【easy】118.119.杨辉三角

    这题必会啊!!! 第一题118. class Solution { public: vector<vector<int>> generate(int numRows) { ve ...

  6. 【LeetCode】122.Best Time to Buy and Sell Stock II 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. 【leetcode】Populating Next Right Pointers in Each Node I & II(middle)

    Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...

  8. 【LeetCode】122. Best Time to Buy and Sell Stock II

    题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...

  9. 【LeetCode】462. 最少移动次数使数组元素相等 II

    给定一个非空整数数组,找到使所有数组元素相等所需的最小移动数,其中每次移动可将选定的一个元素加1或减1. 您可以假设数组的长度最多为10000. 例如: 输入: [1,2,3] 输出: 2 说明: 只 ...

随机推荐

  1. Oracle ->> 行转列, 列转行

    除了Pivot和Unpivot这两个函数,还有像CASE WHEN + 聚合函数像MAX,SUM这类的来完成.今天发现Oracle下居然有这样一个和SQL SERVER 2012以后新增的新函数叫II ...

  2. 每个PHP开发者都应该看的书

    PHP这几年口碑很差.关于它的“糟糕设计的汇总”和语法上的矛盾有着大量的讨论,但是主要的抱怨通常是安全.很多PHP站点分分钟被黑掉,甚至一些有经验的.有见识的程序员会说,这门语言本身是不安全的. 我总 ...

  3. spring各个包之间的依赖关系

    从图中可以看到: 1.spring core,spring beans被其他较多包依赖,spring aop,spring context,spring expression分别被两个包依赖,而spr ...

  4. NDK(8)"Unknown Application ABI"的解决方案

    ndk 调试本地应用时 报错如下 : console信息: [2015-08-17 19:52:05 - NdkSample] Unknown Application ABI: [2015-08-17 ...

  5. 网易云课堂学习之VS相关

    1.为开发好的项目文件瘦身 如:在项目文件ScreenCapture中,只需保留框起来的两个文件即可 而且在框起来的ScreenCapture里的Debug文件也可以删掉,整个文件由75.4 MB变为 ...

  6. webhdfs追加写HDFS异常

    问题 {:timestamp=>"2015-03-04T00:02:47.224000+0800", :message=>"Retrying webhdfs ...

  7. hdu4760Good Firewall

    4760 数组模拟就可以了 读的时候可以整数读入 #include <iostream> #include<cstdio> #include<cstring> #i ...

  8. git remotes

    简单地说,一个remote repository是一个非本地的repo.它可以是在你公司网络上的另外一个git repo,也可以是在internet上,甚至在你本地文件系统中的一个repo,关键点是它 ...

  9. const,readonly 常量与只读

    Const是常量 Const在编译时会被编译为静态成员,它确定于编译时期,属类型级,通过类型来访问. 现在通过以下几种情况来说明const常量: (1)初始化 public const string  ...

  10. HDU 3666 THE MATRIX PROBLEM (差分约束,最短路)

    题意: 给一个n*m矩阵,每个格子上有一个数字a[i][j],给定L和U,问:是否有这样两个序列{a1...an}和{b1...bn},满足 L<=a[i][j]*ai/bj<=U .若存 ...