python leetcode 118. Pascal's Triangle 119. Pascal's Triangle II

挺有趣的题目

118. Pascal’s Triangle

class Solution(object):
    def generate(self, numRows):
        """
        :type numRows: int
        :rtype: List[List[int]]
        """
        ln=numRows
        if ln==0:
            return []
        tmp=[0,1]
        res=[]
        for i in range(ln):
            curr=[]
            for i in range(len(tmp)-1):
                curr.append(tmp[i]+tmp[i+1])
            res.append(curr)
            tmp=[0]+curr[:]+[0] 
        return res

119. Pascal’s Triangle II

class Solution(object):
    def getRow(self, rowIndex):
        """
        :type rowIndex: int
        :rtype: List[int]
        """
        ln=rowIndex
        
        tmp=[0,1]
        
        for i in range(ln+1):
            curr=[]
            for i in range(len(tmp)-1):
                curr.append(tmp[i]+tmp[i+1])
            tmp=[0]+curr[:]+[0] 
        return curr

猜你喜欢

转载自blog.csdn.net/Neekity/article/details/84974565