【LeetCode OJ】Pascal's Triangle II
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的更多相关文章
- 【LeetCode OJ】Pascal's Triangle
Prolbem Link: http://oj.leetcode.com/problems/pascals-triangle/ Just a nest-for-loop... class Soluti ...
- 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, ...
- 【LeetCode OJ】Linked List Cycle II
Problem link: http://oj.leetcode.com/problems/linked-list-cycle-ii/ The solution has two step: Detec ...
- 【LeetCode】Pascal's Triangle II 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
- 【leetcode】Pascal's Triangle II
题目简述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Retur ...
- 【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 ...
- 【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, ...
- 【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, ...
- LeetCode OJ 118. Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
随机推荐
- 超实用的JavaScript代码段 Item8 -- js对象的(深)拷贝
js 对象 浅拷贝 和 深拷贝 1.浅拷贝 拷贝就是把父对像的属性,全部拷贝给子对象. 下面这个函数,就是在做拷贝: var Chinese = { nation:'中国' } var Doctor ...
- IKAnalyzer进行中文分词和去停用词
最近学习主题模型pLSA.LDA,就想拿来试试中文.首先就是找文本进行切词.去停用词等预处理,这里我找了开源工具IKAnalyzer2012,下载地址:(:(注意:这里尽量下载最新版本,我这里用的IK ...
- px和em的区别
px和em的区别 2012-06-21 23:01:06| 分类: CSS|字号 订阅 在如今这个提倡可用性设计以及用户体验设计的网络时代,CSS也是要一同参与其中的.大部分人在CSS代码编写中总是 ...
- Android开发--TextView的应用
1.概述 TextView主要用于Activity中文本的应用.其中layout中xml文件(activity)设置文本的宽度,高度,ID:values中strings.xml设置文本内容. Text ...
- java--常用类summary(三)
/* 1:正则表达式(理解) (1)就是符合一定规则的字符串 (2)常见规则 A:字符 x 字符 x.举例:'a'表示字符a \\ 反斜线字符. \n 新行(换行)符 ('\u000A') \r 回车 ...
- BZOJ1570 [JSOI2008]Blue Mary的旅行
建分层图,每一层表示一天的情况 从S向第0层的1号点连边,每层的n向T连INF的边 枚举天数,每多一天就多建一层然后跑最大流,如果当前流量大于人数则输出答案 由于路径长度不会超过n,因此tot个人走这 ...
- Ubuntu里面的安装命令总结
本人是新手中的新手,才开始用ubuntu.下面,总结一下安装软件的方法...... 0. 利用apt-get 其实,在ubuntu下安装软件的方法其实灰常简单.就是在终端里面输入: sudo apt- ...
- java面向对象编程— —第七章 继承
7.1继承的起源 继承(Inheritance),即在面向对象编程中,可以通过扩展(extends)一个已有的类,并继承该类的属性的行为,来创建一个新的类. 已有的类称为父类(也可以称为基类,超类), ...
- 百度上传android包:应用名解析失败!
manifest 里面<application增加android:label="@string/app_name"
- C#点击按钮用DataGridView动态增加行、删除行,增加按钮列
原来有一行: 点击添加,在下面增加同样的一行 新增加的行有一列删除按钮,点击某行的删除按钮时,删除当前行 方法: 哈哈,我果然好聪明啊 1.文本框.文本框.添加按钮 2.一个DataGridView( ...