码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode#118 Pascal's Triangle

时间:2015-07-21 21:47:31      阅读:93      评论:0      收藏:0      [点我收藏+]

标签:

Problem Definition:

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]
]


Solution: 关于杨辉三角

 1 def generate(numRows):
 2         pt=[]
 3         for rn in range(numRows):
 4             row=[0]*(rn+1)
 5             row[0],row[rn]=1,1    
 6             for cl in range(1,rn):
 7                 p1=pt[rn-1][cl-1]
 8                 p2=pt[rn-1][cl]
 9                 row[cl]=p1+p2
10             pt+=[row]
11         return pt

 




LeetCode#118 Pascal's Triangle

标签:

原文地址:http://www.cnblogs.com/acetseng/p/4665500.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!