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]
]
class Solution:
# @return a list of lists of integers def generate(self, numRows):
def g(ls):
res = [1]
left = 1
for i in range(1,len(ls)):
res.append(left+ls[i])
left = ls[i]
res.append(1)
return res res = []
if numRows < 3:
if numRows == 0:
return res
for i in range(1,numRows+1):
res.append([1]*i)
else:
res.append([1])
res.append([1,1]) for i in range(2,numRows):
res.append(g(res[i-1]))
return res

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?

class Solution:
# @return a list of integers
def getRow(self, rowIndex):
def g(ls):
res = [1]
left = 1
for i in range(1,len(ls)):
res.append(left+ls[i])
left = ls[i]
res.append(1)
return res res = []
if rowIndex< 2:
res = [1]*(rowIndex+1)
else:
res.append([1,1])
for i in range(rowIndex):
res = (g(res))
return res

LeetCode Pascal's Triangle && Pascal's Triangle II Python的更多相关文章

  1. [leetcode]Find Minimum in Rotated Sorted Array II @ Python

    原题地址:https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 解题思路:这道题和上一道题的区别是,数组中 ...

  2. 28. Triangle && Pascal's Triangle && Pascal's Triangle II

    Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to a ...

  3. LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>

    LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...

  4. LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++>

    LeetCode 80 Remove Duplicates from Sorted Array II [Array/auto] <c++> 给出排序好的一维数组,如果一个元素重复出现的次数 ...

  5. [leetcode]Word Ladder II @ Python

    [leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...

  6. Leetcode之二分法专题-275. H指数 II(H-Index II)

    Leetcode之二分法专题-275. H指数 II(H-Index II) 给定一位研究者论文被引用次数的数组(被引用次数是非负整数),数组已经按照升序排列.编写一个方法,计算出研究者的 h 指数. ...

  7. Leetcode之回溯法专题-90. 子集 II(Subsets II)

    Leetcode之回溯法专题-90. 子集 II(Subsets II) 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入 ...

  8. Leetcode之回溯法专题-47. 全排列 II(Permutations II)

    Leetcode之回溯法专题-47. 全排列 II(Permutations II) 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2] ...

  9. 前端与算法 leetcode 350. 两个数组的交集 II

    目录 # 前端与算法 leetcode 350. 两个数组的交集 II 题目描述 概要 提示 解析 解法一:哈希表 解法二:双指针 解法三:暴力法 算法 # 前端与算法 leetcode 350. 两 ...

  10. 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)

    [LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...

随机推荐

  1. Eclipse 设置代码风格

    自动调整代码风格 快捷键Ctrl + Shift + F 或者 右键 source -> format 设置代码风格 window -> preference -> java -&g ...

  2. t-SNE 聚类

    一个有效的数据降维的方法 t-SNE,类似PCA的主成分降维分析. 参考: t-分布邻域嵌入算法(t-SNE algorithm)简单理解 t-SNE初学 很好的教程:An illustrated i ...

  3. Java中/r和/n的区别

    /n换行符,效果是新换一行,光标在原有位置下一行 /r回车符,效果是光标来到下一行行首

  4. CF808D STL

    D. Array Division time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  5. 使用GAN进行异常检测——可以进行网络流量的自学习哇,哥哥,人家是半监督,无监督的话,还是要VAE,SAE。

    实验了效果,下面的还是图像的异常检测居多. https://github.com/LeeDoYup/AnoGAN https://github.com/tkwoo/anogan-keras 看了下,本 ...

  6. 使用iview-project 打包build报错,ERROR in xxxxx.cheunk.js from UglifyJs

    一.iview-project  为iview官方推荐工程,一个基于iview的vue脚手架 github网址:https://github.com/iview/iview-project 废话不多说 ...

  7. bzoj1008

    题解: 要求有几种方案可以越狱,可以用总方案-不会越狱的方案 那么总方案就是m^n 那么考虑不会越狱的方案 显然第一个人有m中,后面都是m-1中(和前一个不一样) 答案就是m^n-m*(m-1)^(n ...

  8. C语言编程的环境以及架构

    c程序的使用步骤:

  9. 高版本的jdk编译过的项目移到低版本的JDK的eclipse中出错的问题

    由于2台电脑安装的jdk版本不一样,导致从一台电脑移动项目到另一台电脑上时,运行出现了错误,错误信息如下: 主要是原先项目运行的JDK版本为1.8, 而要移过去的电脑的jdk是1.7的,首先已经把bu ...

  10. 【设计模式】 模式PK:工厂模式VS建造者模式

    1.概述 工厂方法模式注重的是整体对象的创建方法,而建造者模式注重的是部件构建的过程,旨在通过一步一步地精确构造创建出一个复杂的对象.我们举个简单例子来说明两者的差异,如要制造一个超人,如果使用工厂方 ...