Given numrows, generate the first numrows of Pascal ' s triangle.
For example, given numrows = 5,
Return
[ 1], [ 1,2,1], [ 1,3,3,1], [ 1,4,6,4,1]
Subscribe to see which companies asked this question
Problem Solving Analysis:
The subject of this Pascal (1623----1662) was discovered in 1654, 393 years later than Yang Hui, 600 years later than Jia Xian.
The concrete algorithm is as follows, we discuss in two kinds of situations,
1, the first element [1].
2, starting from the second element to match the sum of the above results, and finally at the end of a 1
Then we first result.append ([1]). Then we make a judgment and execute the formula:
Ans = result[i].append (Result[i-1][j] + result[i-1][j-1])
#-*-Coding:utf-8-*-__author__ = ' Jiuzhang ' class solution (object): def generate (self, numrows): result = []
for i in range (numrows): result.append ([1]) for J in range (1, i + 1): if J = = 1: result.append (1) El SE: result[i].append (Result[i-1][j] + result[i-1][j-1]) return result
(Leetcode) Pascal ' s Triangle---yang Hui triangle