<LeetCode><Easy> 118 Pascal's Triangle II
2015-10-16 18:24
393 查看
Given an index k, return the kth row of the Pascal's triangle.
For example, given k = 3,
Return
Note:
Could you optimize your algorithm to use only O(k) extra space?
#Python2 TimeOut
#递归
偏移求和【0】+L(k) 与L(k)+【0】--> L(k+1)
class Solution(object):
def getRow(self, rowIndex):
"""
:type rowIndex: int
:rtype: List[int]
"""
get=lambda k:map(lambda x,y:x+y,get(k-1)+[0],[0]+get(k-1)) if k>1 else [1,1] if k==1 else [1]
get(rowIndex)
#是组合数的列写
class Solution(object):
def getRow(self, rowIndex):
"""
:type rowIndex: int
:rtype: List[int]
"""
fatorial=lambda x:fatorial(x-1)*x if x>1 else 1
combination=lambda m,n:fatorial(m)/fatorial(n)/fatorial(m-n)
if rowIndex%2:
fore=[combination(rowIndex,i) for i in range(rowIndex/2+1)]
return fore+fore[::-1]
else:
fore=[combination(rowIndex,i) for i in range(rowIndex/2+1)]
return fore+fore[::-1][1:]
For example, given k = 3,
Return
[1,3,3,1].
Note:
Could you optimize your algorithm to use only O(k) extra space?
#Python2 TimeOut
#递归
偏移求和【0】+L(k) 与L(k)+【0】--> L(k+1)
class Solution(object):
def getRow(self, rowIndex):
"""
:type rowIndex: int
:rtype: List[int]
"""
get=lambda k:map(lambda x,y:x+y,get(k-1)+[0],[0]+get(k-1)) if k>1 else [1,1] if k==1 else [1]
get(rowIndex)
#是组合数的列写
class Solution(object):
def getRow(self, rowIndex):
"""
:type rowIndex: int
:rtype: List[int]
"""
fatorial=lambda x:fatorial(x-1)*x if x>1 else 1
combination=lambda m,n:fatorial(m)/fatorial(n)/fatorial(m-n)
if rowIndex%2:
fore=[combination(rowIndex,i) for i in range(rowIndex/2+1)]
return fore+fore[::-1]
else:
fore=[combination(rowIndex,i) for i in range(rowIndex/2+1)]
return fore+fore[::-1][1:]
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例
- Python 七步捉虫法