Pascal's Triangle

Problem: Pascal's Triangle

Based on the principle of Pascal's Triangle, the values of the head and tail element are one. The other values can be calculated as the sum of the 2 elements above it.

Code in Python:

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

results matching ""

    No results matching ""