Pascal's Triangle II

    这是一个简单题

  题目:

    

 

  思路:

    我的:这个题和Pascal's Triangle的思路是一样的,只不过这个题求的是其中的一层,代码差不多只不过这个题索引是从0开始的

    大神的: 用的是列表生成器和一个zip函数

  代码:、

    我的:

 1 class Solution(object):
 2     def getRow(self, n):
 3         """
 4         :type rowIndex: int
 5         :rtype: List[int]
 6         """
 7         a = [[0] for i in xrange(n+1)]
 8         a[0] = [0,1]
 9         for i in xrange(1, n+1):
10             for j in xrange(len(a[i-1])):
11                 a[i].append(a[i-1][j]+a[i-1][len(a[i-1])-j-1])
12         for i in xrange(n+1):
13             a[i].pop(0)
14         return a[n]

    大神:

 1 class Solution(object):
 2     def getRow(self, rowIndex):
 3         """
 4         :type rowIndex: int
 5         :rtype: List[int]
 6         """
 7         row = [1]
 8         for _ in range(rowIndex):
 9             row = [x + y for x, y in zip([0]+row, row+[0])]
10         return row

 

posted @ 2017-09-26 15:11  唐僧洗发爱飘柔  阅读(92)  评论(0)    收藏  举报