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的更多相关文章

  1. LeetCode:Pascal's Triangle I II

    LeetCode:Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For examp ...

  2. [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, ...

  3. [LeetCode] Pascal's Triangle 杨辉三角

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  4. [leetcode]Pascal's Triangle II @ Python

    原题地址:https://oj.leetcode.com/problems/pascals-triangle-ii/ 题意: Given an index k, return the kth row ...

  5. [leetcode]Pascal's Triangle @ Python

    原题地址:https://oj.leetcode.com/problems/pascals-triangle/ 题意: Given numRows, generate the first numRow ...

  6. 学会从后往前遍历,例 [LeetCode] Pascal's Triangle II,剑指Offer 题4

    当我们需要改变数组的值时,如果从前往后遍历,有时会带来很多麻烦,比如需要插入值,导致数组平移,或者新的值覆盖了旧有的值,但旧有的值依然需要被使用.这种情况下,有时仅仅改变一下数组的遍历方向,就会避免这 ...

  7. LeetCode - Pascal's Triangle II

    题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return ...

  8. LeetCode: Pascal's Triangle II 解题报告

    Pascal's Triangle II Total Accepted: 19384 Total Submissions: 63446 My Submissions Question Solution ...

  9. LeetCode: Pascal's Triangle 解题报告

    Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given ...

随机推荐

  1. 如何使用Valgrind memcheck工具进行C/C++的内存泄漏检测

      系统编程中一个重要的方面就是有效地处理与内存相关的问题.你的工作越接近系统,你就需要面对越多的内存问题.有时这些问题非常琐碎,而更多时候它会演变成一个调试内存问题的恶梦.所以,在实践中会用到很多工 ...

  2. IIS支持下载.config后缀名的文件

    这里用.config举例,其他类型文件同理. 设置MIME点击右侧添加,文件扩展名填写.config,MIME类型填写application/octet-stream.或者添加.*,将所有未列出的文件 ...

  3. My.Ioc 代码示例——避免循环依赖

    本文的目的在于通过一些示例,向大家说明 My.Ioc 支持哪些类型的依赖关系.也就是说,如何设计对象不会导致循环依赖. 在 Ioc 世界中,循环依赖是一个顽敌.这不仅因为它会导致 Ioc 容器抛出异常 ...

  4. Swift - 29 - 参数的默认值

    // 参数设置了默认值之后, 在调用的时候, 可以写这个参数 // 在参数前面添加"_", 表示取消外部参数名, 但还是建议使用苹果默认格式 func sayHello(nickN ...

  5. 模拟vector

    实现了vector的模板,insert, erase, push_back, iterator #include<iostream> #include<string.h> #i ...

  6. Unique Binary Search Tree

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  7. 各种开源协议介绍 BSD、Apache Licence、GPL V2 、GPL V3 、LGPL、MIT

    现今存在的开源协议很多,而经过Open Source Initiative组织通过批准的开源协议目前有58种(http://www.opensource.org/licenses /alphabeti ...

  8. underscorejs-invoke学习

    2.13 invoke 2.13.1 语法: _.invoke(list, method, *args) 2.13.2 说明: 每个list属性值都执行method方法,根据method方法返回一个数 ...

  9. javascript-Cookie的应用

    在我平时开发网页的过程中,可能涉及到浏览器本地的存储,现在主流的浏览器存储方式有:cookie,直接读取xml,userData,H5 的LocalStorage等,Cookie存储数据有限,但对于数 ...

  10. 用批处理来重启IIS的应用程序池

    批处理很简单,先Stop再Start就行,代码如下(apppoolName是应用程序池的实例名):c:\windows\system32\inetsrv\AppCmd.exe stop apppool ...