pascals-triangle-ii leetcode C++

mac2022-06-30  96

Given an index k, return the k th row of the Pascal's triangle.

For example, given k = 3, Return[1,3,3,1].

Note: Could you optimize your algorithm to use only O(k) extra space?

C++

class Solution { public: vector<int> getRow(int rowIndex) { vector<int> dp; dp.push_back(1); for(int i = 1; i <= rowIndex;i++){ vector<int> tmp; tmp.push_back(1); for(int j = 1; j < i; j++) tmp.push_back(dp[j]+ dp[j-1]); tmp.push_back(1); dp = tmp; } return dp; } };

 

转载于:https://www.cnblogs.com/vercont/p/10210267.html

最新回复(0)