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. 【C语言天天练(二一)】内联函数

            引言:调用函数时,一般会由于建立调用.传递參数.跳转到函数代码并返回等花费掉一些时间,C语言的解决的方法是使用类函数宏.在C99中,还提出了第二种方法:内联函数.         内联 ...

  2. const常量折叠

    首先来看一个例子: int main(int argc, char* argv[]) { ; int *j = (int *) &i; *j=; cout<<&i<& ...

  3. chmod -x chmod的N种解法

    声明:该文章摘自陈皓的酷壳. 问题: 如果某天你的Unix/Linux系统上的chomd命令被某人去掉了x属性(执行属性),那么,你如何恢复呢? 参考答案: 1)重新安装.对于Debian的系统: s ...

  4. NSIndexPath初始化

    在UITableView中经常用到这个类,但一直不知道怎么初始化,网上抄录的代码如下,果然好用 NSIndexPath *index = [NSIndexPath indexPathForRow:0 ...

  5. Android Studio 使用GitHub

    Android Studio 使用GitHub 1.安装配置 默认大家都已经安装了git软件,参考下图进行git与as关联 配置git  设置GitHub用户信息  填写完用户名,密码后可以点击Tes ...

  6. bootstrap的流式布局

    Bootstrap---Fluid layout 流布局 流布局是一种适应屏幕的做法.即不固定块的宽度,而是采用百分比作为单位来确定每一块的宽度.这种布局非常适合一次编写,然后自适应各种不同大小的屏幕 ...

  7. HTML之Data URL(转)

    Data URL给了我们一种很巧妙的将图片“嵌入”到HTML中的方法.跟传统的用img标记将服务器上的图片引用到页面中的方式不一样,在Data URL协议中,图片被转换成base64编码的字符串形式, ...

  8. C#中创建线程,创建带参数的线程

    线程操作主要用到Thread类,他是定义在System.Threading.dll下.使用时需要添加这一个引用.该类提供给我们四个重载的构造函 构造函数定义: 无参数委托 [SecuritySafeC ...

  9. Swift - 15 - 导入Foundation使用更多字符串功能

    //: Playground - noun: a place where people can play import Foundation var str = "Hello, playgr ...

  10. Qt远程机开发时光标注意问题

    最近项目中有一个比较奇怪的问题,就是当记录了最后的m_lastPos为当前widget中间位置之后,设置了QCursor也为当前中间位置. 这个时候当开始移动的时候,发现offset出现了很怪的极大值 ...