[Array]Pascal's Triangle II

mac2022-06-30  92

Given an index k, return the kth 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?

方法:在每一行的更新中,从后往前进行更新可以使代码更加简洁。

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

转载于:https://www.cnblogs.com/GoFly/p/5751060.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)