118. Pascal's Triangle Leetcode Python
来源:互联网 发布:大数据专业课程 编辑:程序博客网 时间:2024/05/26 08:42
Given numRows, generate the first numRows of Pascal's triangle.
For example, given numRows = 5,
Return
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
For example, given numRows = 5,
Return
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
这道题是构造pascal 三角形主要是要建立一个prerow 一个currow 然后每次下一行的除了开始以及结束的位置,中间位置currow.append(prerow[pos]+prerow[pos+1])最后再在最后补一个1
we need to define a prerow and currow everytime we add 1 to currow first and close it with 1, beside that currow.append(prerow[pos]+prerow[pos+1])
the code is as follow:
class Solution: # @return a list of lists of integers def generate(self, numRows): solution=[] if numRows==0: return solution if numRows>0: actualrow=[1] solution.append(actualrow) for index in range(1,numRows): prerow=actualrow actualrow=[1] for j in range(0,index-1): actualrow.append(prerow[j]+prerow[j+1]) actualrow.append(1) solution.append(actualrow) return solution
0 0
- 118. Pascal's Triangle Leetcode Python
- [leetcode: Python] 118.Pascal's Triangle
- 【Leetcode】【python】Pascal's Triangle, Pascal's Triangle II
- LeetCode 118. Pascal's Triangle
- [LeetCode]118.Pascal's Triangle
- 【LeetCode】118.Pascal's Triangle
- [Leetcode] 118. Pascal's Triangle
- 【leetcode】118. Pascal's Triangle
- 【Leetcode】118. Pascal's Triangle
- 118. Pascal's Triangle LeetCode
- 【LeetCode】118. Pascal's Triangle
- [LeetCode]118. Pascal's Triangle
- Leetcode 118. Pascal's Triangle
- LeetCode *** 118. Pascal's Triangle
- leetcode 118. Pascal's Triangle
- LeetCode 118. Pascal's Triangle
- leetcode-118. Pascal's Triangle
- 118.[Leetcode]Pascal's Triangle
- Hive笔记
- Linux网络技术/软件列表——留待以后有空或需要再逐个学习
- Objective - C基础: 第二天 - 11.继承的关键字Super
- Qt使用第三方库GMP,用以超长整数运算
- 使用HBase EndPoint(coprocessor)进行计算
- 118. Pascal's Triangle Leetcode Python
- 把一个数的第三位进行清零、置位、取反
- Android存储理解和操作
- 利用sqoop将hive数据导入导出数据到mysql
- 自定义适配器
- container_of宏详解
- pb通过jdbc连接mysql
- device_register()分析
- 设备驱动基础0:设备模型之kobject,kset及其关系