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. 手势跟踪论文学习:Realtime and Robust Hand Tracking from Depth(三)Cost Function

    iker原创.转载请标明出处:http://blog.csdn.net/ikerpeng/article/details/39050619 Realtime and Robust Hand Track ...

  2. offsetLeft,Left,clientLeft具体解释

      假设 obj 为某个 HTML 控件. obj.offsetTop 指 obj 相对于版面或由 offsetParent 属性指定的父坐标的计算上側位置,整型,单位像素. obj.offsetLe ...

  3. 黑马day14 过滤器概述&amp;生命周期&amp;运行过程

    过滤器:当訪问一个web资源的时候,过滤器就在你訪问这个web资源的前进行拦截...在放行过后...filter过滤器也能够做一些其它的事情. 编写过滤器的步骤: 1.写一个过滤器类实现filter接 ...

  4. Spring25大面试题

    1.什么是Spring框架?Spring框架有哪些主要模块? Spring框架是一个为Java应用程序的开发提供了综合.广泛的基础性支持的Java平台.Spring帮助开发人员攻克了开发中基础性的问题 ...

  5. (OpenGL ES 2.0 Shading Language) attribute 、uniform 和 varying

    一:attribute .uniform 和 varying 都是glsl的变量的内存指示器(storage qualifiers),指明变量的内存特性 二:attribute attribute 是 ...

  6. android studio 、 as 如何导入eclipse项目

    安卓项目有两种,一种是eclipse开发的,一种的android studio开发的.有些在github开源的安卓项目,下载下来之后不知道该如何处理了. 这个是Eclipse安卓项目的目录结构. 这个 ...

  7. VC++基于CXImage库实现缩略图

    一般的图像处理软件都对读入程序的图像文件建一个缩略图的列表,像ACDSee那样.笔者最近在做一个图像处理的项目,处理的原始数据就是图像文件.从项目一开始就想做一个缩略图,但一直苦于技术水平有限,且时间 ...

  8. 1.future线程通信

    #include <future> #include<iostream> #include <thread> #include <thread> #in ...

  9. 07:清泉-改(prime+堆)

    时间限制:  10000ms 单个测试点时间限制:  1000ms 内存限制:  512000kB 描述 华北电力大学可以抽象为一张有n个点m条边的无向图. 现在所有的边都断了. 修复每条边都有个不同 ...

  10. Codeforces 982 B. Bus of Characters(模拟一个栈)

    解题思路: 排序之后模拟一个栈(也可以用真的栈),时间复杂度o(n). 代码: #include <bits/stdc++.h> using namespace std; typedef ...