leetcode-easy-others-118 Pascal's Triangle

mycode   16.47%

class Solution(object):
    def generate(self, numRows):
        """
        :type numRows: int
        :rtype: List[List[int]]
        """
        if not numRows : return []
        elif numRows == 1 : return [[1]]
        res = [[1],[1,1]]
        for i in range(2,numRows):
            temp = [1]
            for j in range(1,i):
                temp.append(res[i-1][j-1] + res[i-1][j])
            temp.append(1)
            res.append(temp)
        return res

参考

class Solution(object):
    def generate(self, numRows):
        """
        :type numRows: int
        :rtype: List[List[int]]
        """
        triangle=[[1]*n for n in range(1,numRows+1)]
        
        for i in range(2,numRows):  #第三行开始
            for j in range(1,i):  #第二列到倒数第二列
                triangle[i][j]=triangle[i-1][j-1]+triangle[i-1][j]           
        return triangle

猜你喜欢

转载自www.cnblogs.com/rosyYY/p/11006191.html