LeetCode——Pascal's Triangle
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]
] https://oj.leetcode.com/problems/pascals-triangle/ 杨辉三角,先把两遍的值设为1,然后算中间的就是上一层的两个数相加,算当前层的时候,上一层下标一定是从0开始一直加到最后一个。 Python代码
class PascalsTriangle:
def generate(self, numRows):
res = []
for i in range(1, numRows+1):
tem = [0 for x in range(i)]
if i == 1:
tem[0] = 1
# print(tem[0])
res.append(tem)
else:
pos = 0
for j in range(1, i+1):
if j == 1:
tem[0] = 1
elif j == i:
tem[j - 1] = 1
else:
tem[j-1] = res[i-2][pos] + res[i-2][pos+1]
pos += 1
res.append(tem)
return res p = PascalsTriangle()
p.generate(5)
LeetCode——Pascal's Triangle的更多相关文章
- LeetCode:Pascal's Triangle I II
LeetCode:Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For examp ...
- [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] Pascal's Triangle 杨辉三角
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- [leetcode]Pascal's Triangle II @ Python
原题地址:https://oj.leetcode.com/problems/pascals-triangle-ii/ 题意: Given an index k, return the kth row ...
- [leetcode]Pascal's Triangle @ Python
原题地址:https://oj.leetcode.com/problems/pascals-triangle/ 题意: Given numRows, generate the first numRow ...
- 学会从后往前遍历,例 [LeetCode] Pascal's Triangle II,剑指Offer 题4
当我们需要改变数组的值时,如果从前往后遍历,有时会带来很多麻烦,比如需要插入值,导致数组平移,或者新的值覆盖了旧有的值,但旧有的值依然需要被使用.这种情况下,有时仅仅改变一下数组的遍历方向,就会避免这 ...
- LeetCode - Pascal's Triangle II
题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return ...
- LeetCode: Pascal's Triangle II 解题报告
Pascal's Triangle II Total Accepted: 19384 Total Submissions: 63446 My Submissions Question Solution ...
- LeetCode: Pascal's Triangle 解题报告
Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given ...
随机推荐
- [转] Are You Making a Big Mistake in This Volatile Market?
Stock market volatility continues unabated. It may be too early to tell, but I’m marking the top of ...
- NYOJ2括号配对问题
括号配对是最基本的栈的问题,它是栈入门的经典题目,思路是,如果是左括号直接进栈,如果是右括号,这时就要比较栈顶的元素与他是否匹配,如果匹配则出栈,否则进栈,下面是代码的实现: #include < ...
- Filter过滤器实现同一地址手机和电脑页面不同
最近做一个网站,客户要求在访问主域名的时候实现电脑访问时展示电脑页面,手机访问时展示h5的手机页面,这种需求的使用还是比较多的:尤其网站需要百度推广的时候,百度推广就要求同一域名下,手机访问时展示手机 ...
- IIS相关问题
问题:使用vs开发项目完成后,发布在本地IIS上,访问链接出现如下情况: 解决方案:打开IIS--->>
- JAVA 读取pdf文件
第一个路口action /* * wuhan syspro author zhangrui 2010/08/23 */ package jp.co.syspro.poo.action; import ...
- Wpf 数据绑定之BindingBase.StringFormat
BindingBase.StringFormat 属性获取或设置一个字符串,该字符串指定如果绑定值显示为字符串,应如何设置该绑定的格式. StringFormat 可以是预定义的.撰写的或自定义的字符 ...
- asp.net中Request.ServerVariables的用法
在asp.net中可以通过HttpRequest.ServerVariables 属性来获取“ Web 服务器变量的集合” HttpRequest.ServerVariables 的用法: HttpR ...
- struts2与spring整合问题,访问struts2链接时,spring会负责创建Action
每次访问一次链接,spring会创建一个对象,并将链接所带的参数注入到Action的变量中(如何做到的呐) 因为: struts2的action每次访问都重新创建一个对象,那spring的ioc是怎么 ...
- Ajax简单应用-购物车
1. 2. 3. 4. 5. 6.
- dede 留言板访问的目录
D:\APMServ5.2.6\www\htdocs\xyhy\templets\plus guestbook.rar 文件 里面 DEDE留言簿的插件: