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. (13)odoo翻译

    -------------------更新时间:15:52 2016-09-28 星期三 增加模型名翻译17:26 2016-05-20 星期五17:58 2016-05-17 星期二12:14 20 ...

  2. 【Java基础之Object类(一)】Java中Object类中的所有方法(toString、equals、hashCode、clone、finalize、wait和notify等)详解(转载)

    java中的hashcode.equals和toString方法都是基类Object的方法. 首先说说toString方法,简单的总结了下API说明就是:返回该对象的字符串表示,信息应该是简明但易于读 ...

  3. Oracle 表死锁 解决

    问题:更新的Update语句一直在更新 卡在执行update语句的地方. 清除的方法: Oracle表死锁解除   我是在plsql中处理  1.先查询  select * from v$locked ...

  4. abap注意

    1.建表的时候所有的数据元素的总长度不能超过1024. 2.表的主键修改在se11激活不成功,但是可以在se11保存,然后到se14中激活. 3.SM12解锁,在很多时候,经常出现某个表或者可修改的地 ...

  5. 安装VC6提示找不到ACME时的解决办法

    将安装程序COPY到电脑上1.打开setupwiz.ini,把"acme=acmboot.exe"改为"=acmsetup.exe";2.STF=setup/v ...

  6. journal

    dec 5 rpt prep exam dec 4 lie to me dec 3 exam dec 2 preparation for exam dec 1 preparation for exam ...

  7. MVC1-5直接访问静态页面

    MVC模式下默认是无法访问站点内静态页面,昨日百度找了半天试了半天才试成功. 默认在Views文件外的静态页面可以访问,若要访问Views里的静态页面则需要修改View文件夹中的web.config: ...

  8. jQuery给同一个元素两个点击事件

    $(".course-form .course-start img").each(function(i) { $(this).toggle(function(){ $(this). ...

  9. JavaScript基础---作用域,匿名函数和闭包【转】

    匿名函数就是没有名字的函数,闭包是可访问一个函数作用域里变量的函数. 一.匿名函数 //普通函数 function box() { //函数名是 box return 'TT'; } //匿名函数 f ...

  10. 用户列表-投资记录sql

    --普通标.定向标.新手标.老互融计划-投资记录表select bid.borrow_id, (select yyb.borrow_valid_time from YYD_Borrow_BorrowI ...