Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

describe

Given an integer numRows, return the first numRows of Pascal’s triangle.

In Pascal’s triangle, each number is the sum of the two numbers directly above it as shown:

Example 1:

Input: numRows = 5 Output: [[1], [1, 1], [1, 2, 1],,3,3,1 [1], [1,4,6,4,1]]Copy the code

Example 2:

Input: numRows = 1
Output: [[1]]
Copy the code

Note:

1 <= numRows <= 30
Copy the code

parsing

Return the former numRows of Pascal’s Triangle.

As a matter of fact, all those who have received quality education know that this is Yang Hui’s Triangle, but foreigners look at the world from a perspective. PASCAL discovered this law and was afraid that others would take the credit for it, so he named himself and later became PASCAL’s Triangle. In fact, nearly 400 years later than Yang Hui, the pattern of foreigners can be seen.

But after this matter so see also pretty important, if is named Yang hui triangle, you must know that this is a Chinese invention, but clearly into PASCAL’s triangle, many people started drumming in his heart, not sure whether the Chinese invented, so master the naming rights is how important, extend from here again, like all kinds of festival “but we have to fight for, Otherwise, after ten or twenty years, children will think that the Mid-Autumn Festival is South Korea!

The law of Yang Hui’s triangle is also given in the title, that is, each number is the sum of the two numbers directly above it, as shown in the figure:

The first line is [1], the second line is [1,1], and from the third line on, except for the first element and the last element, the middle element is the same rule.

answer

class Solution(object):
    def generate(self, numRows):
        """
        :type numRows: int
        :rtype: List[List[int]]
        """
        result = []
        for i in range(1, numRows+1):
            tmp = [1]
            for j in range(1, i-1):
                tmp.append(result[-1][j-1]+ result[-1][j])
            if i>=2:
                tmp.append(1)
            result.append(tmp)
        return result
                
                
            
        	      
		
Copy the code

The results

Given in the Python online submission to Pascal's Triangle. Memory Usage: 13 MB, less than 37.26% of Python online submissions for Pascal's TriangleCopy the code

Original link: leetcode.com/problems/pa…

Your support is my biggest motivation