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]
]

解决方案:

vector<vector<int>> generate(int numRows) {
vector<vector<int>> res = {};
for (int i = 0; i < numRows; i++) {
res.push_back(vector<int>(i + 1, 1));
for(int j = 1; j < i; j++) {
res[i][j] = (res[i - 1][j] + res[i - 1][j - 1]);
}
}
return res; }

Pascal's Triangle II
Total Accepted: 46342
Total Submissions: 157260

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?

我的解决方案:

从没一行的倒数第二个算起,往前面逆推:

 vector<int> getRow(int rowIndex)
{
vector<int> result(rowIndex + 1, 1); for(int i = 1; i <= rowIndex; ++i)
{
for(int j = i - 1; j > 0; --j)
{
result[j] = result[j] + result[j - 1];
}
} return result;
}

递归的解决方案:

vector<int> getRow(int rowIndex) {
vector<int> result; if (rowIndex == 0) {
result.push_back(1); return result;
} else {
vector<int> vec = getRow(rowIndex - 1);
result.push_back(1);
for (size_t i = 0; i < vec.size() - 1; i++) {
result.push_back(vec[i] + vec[i+1]);
}
result.push_back(1);
}
}

python 解决方案:

class Solution:
# @param {integer} rowIndex
# @return {integer[]}
def getRow(self, rowIndex):
row = [1]
for i in range(1, rowIndex+1):
row = list(map(lambda x,y: x+y, [0]+row, row + [0]))
return row

leetcode 生成杨辉三角形, 118 119 Pascal's Triangle 1,2的更多相关文章

  1. 【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, ...

  2. 118/119. Pascal's Triangle/II

    原文题目: 118. Pascal's Triangle 119. Pascal's Triangle II 读题: 杨辉三角问题 '''118''' class Solution(object): ...

  3. leetcode 生成杨辉三角形, 118 119 Pascal's Triangle 1,2

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...

  4. 118. 119. Pascal's Triangle -- 杨辉三角形

    118. Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, ...

  5. 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- ...

  6. 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, ...

  7. [LeetCode] 119. Pascal's Triangle II 杨辉三角之二

    Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note t ...

  8. [LeetCode]题解(python):119 Pascal's Triangle II

    题目来源 https://leetcode.com/problems/pascals-triangle-ii/ Given an index k, return the kth row of the ...

  9. LeetCode 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, ...

随机推荐

  1. poj 3259 bellman最短路推断有无负权回路

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36717   Accepted: 13438 Descr ...

  2. Knockout源代码精析-怎样解析demo元素,获取到bindings(二)?

    接上文这里開始分析applyBindingsToNodeInternal.applyBindingsToNodeInternal方法例如以下: function applyBindingsToNode ...

  3. bzoj3295: [Cqoi2011]动态逆序对(cdq分治+树状数组)

    3295: [Cqoi2011]动态逆序对 题目:传送门 题解: 刚学完cdq分治,想起来之前有一道是树套树的题目可以用cdq分治来做...尝试一波 还是太弱了...想到了要做两次cdq...然后伏地 ...

  4. Checkmarx VisualStudio plugin installation process.

    1-      Configuration of plugin VSTudio Prerequisite: -Your visual studio MUST be up to date with th ...

  5. 接入gitment为hexo添加评论功能

    title: 接入gitment为hexo添加评论功能 toc: false date: 2018-04-16 10:59:56 categories: methods tags: hexo gitm ...

  6. mybatis与spring整合配置

    mybatis与spring整合配置: 第一种方式:(此处配置扫描的包路径.注解.每个mapper类上面需要加@Repository才能纳入spring的bean管理器中) <!-- 自动扫描m ...

  7. VS2012数据绑定控件DataGridView和DataGrid

    在做Windows窗体上ADO.NET数据绑定试验的时候,发现实例中提到的一些控件在vs2012的工具箱中找不到,开始以为是工具箱中的控件太多没看到,结果重新找还是没找到,难道是因为控件升级了?yes ...

  8. C++之const关键字

    本文引自http://www.cnblogs.com/lichkingct/archive/2009/04/21/1440848.html ,略有增删 const关键字在c++中用法有很多,总结如下: ...

  9. 51nod 1402 最大值 3级算法题 排序后修改限制点 时间复杂度O(m^2)

    代码: 题意,第一个数为0,相邻的数相差0或者1,有一些点有限制,不大于给定值,求这组数中可能的最大的那个数. 这题我们看一个例子:第5个数的限制为2 1 2 3 4 5 6 7 8 9 0 1 2 ...

  10. ASP.NET使用MergeInto做数据同步,同步SQLSERVER不同数据库的相同表结构的数据

    public string SynchronousData() { ReturnJson Rejson = new ReturnJson(); //将WebConfig中的数据库连接name中的值写进 ...