118. Pascal's Triangle

Given a non-negative integer numRows, generate the first numRows of Pascal's triangle.

Example:

Input: 5
Output:
[
     [1],
    [1,1],
   [1,2,1],
  [1,3,3,1],
 [1,4,6,4,1]
]

当i=0,nums[0][0]=1

当i>0,numrow-1>j>0 ,  nums[i][j]=nums[i-1][j-1]+nums[i-1][j]

当i>0,j=0或j=numrow-1,nums[i][j]=1

一共numrow行

每一行j从0到numrow-1

时间复杂度 1+2+····n~n^2/2    O(N^2)

空间复杂度  也是O(N^2)


这里要注意对空矩阵的赋值,不能直接用索引,而是要用append

discuss:



上一行后面加0,和0加上上一行 这两行相加就得到这一行~很机智

时间复杂度和空间复杂度也并没有改进。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值