Pascal's Triangle - LeetCode

Pascal's Triangle - LeetCode

题目:

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

分析:

杨辉三角形,做这些题目深刻感觉到python中list的好处啊.

代码:

class Solution:
    # @return a list of lists of integers
    def generate(self, numRows):
        res = list()
        for i in range(1,numRows+1):
            temp = [1 for j in range(i)]
            if i >= 3:
                for j in range(1,i-1):
                    temp[j] =temp1[j-1]+temp1[j] 
            temp1 =temp
            res.append(temp)
        return res
            


你可能感兴趣的:(LeetCode,python)