Problem Link:

http://oj.leetcode.com/problems/pascals-triangle-ii/

Let T[i][j] be the j-th element of the i-th row in the triangle, for 0 <= j <= i, i = 0, 1, ...

And we have the recursive function for T[i][j]

T[i][j] = 1, if j == 0 or j == i

T[i][j] = T[i-1][j] + T[i-1][j-1]

Simple enough, the python code is as follows.

class Solution:
# @return a list of integers
def getRow(self, rowIndex):
# Spcecial case
if rowIndex < 2:
return [1] * (rowIndex + 1)
# Initial two lists for update
res = []
res.append([1]*(rowIndex+1))
res.append([1]*(rowIndex+1))
current = 0
# Update row by row
for row in xrange(2, rowIndex+1):
for i in xrange(1, row):
res[1-current][i] = res[current][i] + res[current][i-1]
current = 1 - current
# Return the current list
return res[current]

【LeetCode OJ】Pascal's Triangle II的更多相关文章

  1. 【LeetCode OJ】Pascal's Triangle

    Prolbem Link: http://oj.leetcode.com/problems/pascals-triangle/ Just a nest-for-loop... class Soluti ...

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

  3. 【LeetCode OJ】Linked List Cycle II

    Problem link: http://oj.leetcode.com/problems/linked-list-cycle-ii/ The solution has two step: Detec ...

  4. 【LeetCode】Pascal's Triangle II 解题报告

    [LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...

  5. 【leetcode】Pascal's Triangle II

    题目简述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Retur ...

  6. 【LeetCode】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】【Easy】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, ...

  8. 【leetcode刷题笔记】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, ...

  9. LeetCode OJ 118. Pascal's Triangle

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

随机推荐

  1. java之如何实现调用启动一个可执行文件,exe

    /* * 运行可执行文件:.exe * 当要执行一个本地机器上的可执行文件时, * 可以使用java.lang包中的Runtime类,首先使用Runtime类,首先 * 使用Runtime类声明一个对 ...

  2. 手动实现KVO

    前言 KVO(Key-Value Observing, 键值观察), KVO的实现也依赖于runtime. 当你对一个对象进行观察时, 系统会动态创建一个类继承自原类, 然后重写被观察属性的sette ...

  3. 11 自定制shell提示符

    shell提示符  huiubantu@ubuntu:~$ shell提示符保存在PS1变量中 包括用户名,主机名,当前工作目录 可以通过echo命令查看PS1的内容 huiubantu@ubuntu ...

  4. Subgraph Search Over Large Graph Database

    Subgraph Search Over Large Graph Database Problem Definition Given a graph database and a query grap ...

  5. Div样式查看器

    编写div属性时,经常需要尝试不同的样式,可以用Javascript写一个简单的div样式查看器,方便日常操作: <!DOCTYPE html> <html> <head ...

  6. Objective-C:Category

    Category可以在不需要创建子类或是修改原始类的基础上,动态为已经存在的类添加新的行为(方法),,这样可以保证类的原始设计规模较小,功能增加时再逐步扩展:实现了类的相关方法的模块化,把不同的类方法 ...

  7. mvc+ef+oracle环境中报错:ORA-00001: 违反唯一约束条件

    分析原因: 在oracle中,主健不能自动生成,不过可以通过“序列”来实现,如果是这样的话,问题很可能就出在“序列”上了: ORACLE表主键ID突然从已经存在的ID值开始自动生成,导致违反主键唯一性 ...

  8. BZOJ4003 [JLOI2015]城池攻占

    这题有两种做法来着... 第一种就是一开始想到的比较不靠谱,不过貌似可以过掉: 看从$1$号节点开始到$p$号节点最大需要的体力,记录单调上升的体力,询问的时候二分跳着走就可以了 不过精度问题还有可能 ...

  9. android 定制目录

    首先简单介绍一下安卓系统文件夹对照表 主要介绍的是Android系统的文件夹结构,帮助大家更直观地了解系统 \\system\\app这个里面主要存放的是常规下载的应用程序,可以看到都是以APK格式结 ...

  10. cookie与sessionID之间的关系实验

    上一篇介绍了cookie,这里来看看cookie与sessionID之间有什么关系. 一.编写测试用例代码 新建一个servlet如下: public class SessionServlet ext ...