Pascal's Triangle
Given a non-negative integer numRows, generate the first _numRows _of Pascal's triangle.
In Pascal's triangle, each number is the sum of the two numbers directly above it.
Example:
Input:
5
Output:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]Analysis
DP的初始条件也就是第一行[1], 第二行[1, 1]
每一层之间的递推关系很直观:
因此只需要按此写出代码即可。
Solution
Dynamic Programming O(n^2) time (0ms 100% AC)
Another Implementation
DP (LeetCode official answer)
DP - 2D array - O(n^2) time, O(n^2) space
Reference
Last updated
Was this helpful?