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. HTML5自学笔记[ 14 ]canvas绘图基础2

    canvas绘制路径不仅可以绘制直线和多边形,还提供了绘制曲线的方法,利用这些方法可以画出多种曲线效果. 方法1:arc(x,y,r,起始弧度,结束弧度,绘制方向);其中(x,y)为圆心坐标,r为半径 ...

  2. 20145236 《Java程序设计》 第6周学习总结

    20145236 <Java程序设计>第6周学习总结 教材学习内容总结 第十章 输入/输出 InputStream与OutputStream 串流设计的概念 Java将输入/输出抽象化为串 ...

  3. struts2视频学习笔记 07-08(为Action的属性注入值,指定需要Struts 2处理的请求后缀,常用常量)

    课时7 为Action的属性注入值(增加灵活性,适用于经常更改的参数) Struts2为Action中的属性提供了依赖注入功能,在struts2的配置文件中,我们可以很方便地为Action中的属性注入 ...

  4. Check the difficulty of problems(POJ 2151)

    Check the difficulty of problems Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5457   ...

  5. lucene底层数据结构——FST,针对field使用列存储,delta encode压缩doc ids数组,LZ4压缩算法

    参考: http://www.slideshare.net/lucenerevolution/what-is-inaluceneagrandfinal http://www.slideshare.ne ...

  6. EFCode First 导航属性

    首先谈谈自己对EF的接触的过程吧,最先接触EF只是因为EF支持从数据库把关系扒下来,可以省掉自己写Select.Update.Insert这些SQL语句,而且修改非常方便,后来在使用的过程中发现导航属 ...

  7. MapReduce 重要组件——Recordreader组件 [转]

    (1)以怎样的方式从分片中读取一条记录,每读取一条记录都会调用RecordReader类: (2)系统默认的RecordReader是LineRecordReader,如TextInputFormat ...

  8. 使用Lucene.Net管理索引实现搜索

    之前使用一直是没有问题的,只到今天发现删除的时候无法删除,增加的时候却一直在增加,导致搜索的时候可以搜出来很多相同的结果. 小猪决定趁今天这个机会好好的把这个问题给解决了. private void ...

  9. ASP.NET MVC 在子页中引用头文件

    在很多时候我们把网站公共的js.css文件放在模板页中,这样在具体的每一个页面里面就不需要单独引用. ASP.NET WebForm里面使用.site文件. 而在ASP.NET MVC 中使用了类似下 ...

  10. windows常见已知熟悉操作命令

    WIN+R--->输入CMD---->回车有关某个命令的详细信息,请键入 HELP 命令名ASSOC          显示或修改文件扩展名关联.ATTRIB         显示或更改文 ...