Pascal's Triangle

GivennumRows, generate the firstnumRowsof Pascal's triangle.

For example, givennumRows= 5, Return

[
     [1],
    [1,1],
   [1,2,1],
  [1,3,3,1],
 [1,4,6,4,1]
]

第一行和第二行都由1组成,其他行起始和结束都是1。内容由上一行相邻的两个数字之和组成:result[i-1][j-1] + result[i-1][j]

注意:i和j的取值范围

class Solution(object):
    def generate(self, numRows):
        """
        :type numRows: int
        :rtype: List[List[int]]
        """
        result = [[1]*(i+1) for i in xrange(numRows)]
        for i in xrange(1, numRows):
            for j in xrange(1, i):
                result[i][j] = result[i-1][j-1] + result[i-1][j]
        return result

Last updated

Was this helpful?