LeetCode OJ 系列之118 Pascal's Triangle --Python

来源:互联网 发布:左耳最后说了什么 知乎 编辑:程序博客网 时间:2024/05/18 01:06

Problem:

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

Answer:

class Solution(object):    def generate(self, numRows):        """        :type numRows: int        :rtype: List[List[int]]        """        result = [[1 for j in range(i+1)] for i in range(numRows)]        for i in range(len(result)):            for j in range(len(result[i])):                if j == 0 or j == len(result[i])-1: continue                else: result[i][j] = result[i-1][j-1] + result[i-1][j]        return result



0 0