118. Pascal‘s Triangle (DP)
Given an integer numRows, return the first numRows of Pascal's triangle.
we will use the top to bottom DP to do this question. First, from observation, we know that each row's first element and last element are 1.
Other elements follow a rule that dp[i][j] = dp[i - 1][j - 1] + dp[i - 1][j].
Thus, we just need to initilize the first and last element to be 1, and apply the logic above to the rest of the elements.
class Solution {
public:
vector
共有 0 条评论