My python solution with comment


  • 0
    C

    For Pascal's Triangle index calculation, we have an formula for it.
    Index in row k and m th is C(k,m), where C() is combinatorial number. C(k,m) = k*(k-1)...(k-m+1)/(k+1)...*1
    Using this formula, we can solve this problem quickly

       class Solution(object):
            def getRow(self, rowIndex):
                """
                :type rowIndex: int
                :rtype: List[int]
                """
                res = []
                for index in range(rowIndex+1):
                    res.append(self.combNum(rowIndex,index))
                return res
    
        def combNum(self,n,k):
            numerator = 1
            denominator = 1
            for i in range(n-k+1,n+1):
                numerator = numerator * i
            for i in range(1,k+1):
                denominator = denominator * i
            return numerator/denominator

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.