LeetCode动态规划 杨辉三角
生活随笔
收集整理的這篇文章主要介紹了
LeetCode动态规划 杨辉三角
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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:
前幾天做的動態規劃題好難嗚嗚,做道水題找回自信~
狀態轉移方程
triangle[i][j] = triangle[i-1][j-1] + triangle[i-1][j]
邊界條件
triangle[0][0] = triangle[i][0] = triangle[i][i] = 1
代碼
class Solution { public:vector<vector<int>> generate(int numRows) {vector<vector<int>> triangle;vector<int> temp;for(int i = 0; i < numRows; i++){temp.clear();temp.push_back(1);for(int j = 1; j < i; j++){temp.push_back(triangle[i-1][j-1] + triangle[i-1][j]);}if(i != 0)temp.push_back(1);triangle.push_back(temp);}return triangle;} };總結
以上是生活随笔為你收集整理的LeetCode动态规划 杨辉三角的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: b站不能锁屏播放了
- 下一篇: LeetCode动态规划 分割等和子集