118. Pascal’s Triangle
Given numRows, generate the first numRows of Pascal’s triangle.
For example, given numRows = 5,
Return
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
题意:
给定一个行数numrows,产生相应行数的Pascal三角形。
思路:
根据杨辉三角规律,通过上述例子也可以找到规律,从第三行开始,每行除了第一个和最后一个是1外,其他元素等于上一行对应两个元素的和,比如第 i 行 j 列(i > 1 && j > 0),res[i][j] = res[ i - 1 ][ j ] + res[i - 1][ j - 1 ]。
1 | class Solution { |