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

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


 
  

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