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. docker on mac:误删default vm的处理方法

    通过docker-machine 重新建一个vm,在virtualbox里可以直接看到多出来一个vm docker-machine create --driver virtualbox default

  2. EFS 你应该知道的事

    需要备份或者还保留这个路径 %USERPROFILE%\AppData\Roaming\Microsoft\Crypto\RSA certmgr.msc 个人证书导出你开始使用EFS加密后的证书 ci ...

  3. Mac安装软件时 提示已损坏的解决方法

    进入终端: sudo spctl --master-disable

  4. 20 多继承 MRO 算法 深度优先遍历 super

    类的多继承 一个类可以继承多个无关的类. 一个类可以被多个无关的类继承 1.经典类. 在python2.2之前. 已经是历史了. MRO 采用的是树形结构的深度递归遍历(一条道跑到黑) 2.新式类 在 ...

  5. 记一次使用utl_http方法调用接口,报字符或值错误

    背景:ebs系统和其他系统通过utl_http包调用接口,使用log方法记录日志. 某次调用接口,执行到记录日志行报字符或值错误. 查找原因,发现是p_str的长度超过的32767的限制. 解决办法: ...

  6. OAF Sample Code(转)

    原文地址: OAF Sample Code

  7. DB2性能调优

    1.更新统计信息 --更新数据库所有表统计信息 --连接到数据库(-v选项,表示要回显命令,以下同) db2 -v connect to DB_NAME --查看是否收集过统计信息,什么时候更新的   ...

  8. Object是个什么鬼

    引言 老人常说,在js中,一切皆对象,那对象又是什么涅,最常用的我们都知道,对象有方法和属性.由一些键值对构成的集合,然后随便用个大括号括起来就形成了一个对象.看起来蛮简单的,但是真是这么简单么,当我 ...

  9. hadoop kafka install (6)

    reference: http://kafka.apache.org/quickstart  http://dblab.xmu.edu.cn/blog/1096-2/ hadoop@iZuf68496 ...

  10. POJ 2309 BST(树状数组Lowbit)

    题意是给你一个满二叉树,给一个数字,求以这个数为根的树中最大值和最小值. 理解树状数组中的lowbit的用法. 说这个之前我先说个叫lowbit的东西,lowbit(k)就是把k的二进制的高位1全部清 ...