119. Pascal's Triangle II

Posted habibah-chang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了119. Pascal's Triangle II相关的知识,希望对你有一定的参考价值。

rowIndex=0 -> len = 1

所以 例如 rowIndex=3

1.初始化 res=[0,0,0,0],res[0]=1

res = [1,0,0,0]

2.从后往前加,循环 rowIndex-1 次,当前位=当前位+前一位

[1,0,0,0]

[1(不变),1(=1+0),0,0] = [1,1,0,0]

[1(不变),2(=1+1),1(=1+0),0] = [1,2,1,0]

[1(不变),3(=1+2),3(=2+1),1(=1+0)] = [1,3,3,1]

代码参考

 1 class Solution {
 2 public:
 3     vector<int> getRow(int rowIndex) {
 4         vector<int> res(rowIndex+1, 0);
 5         res[0]=1;
 6         for(int i=0; i<=rowIndex; i++){
 7             for(int j=i; j>0; j--){
 8                 res[j]+=res[j-1];
 9             }
10         }
11         return res;
12     }
13 };

 

以上是关于119. Pascal's Triangle II的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode_119. Pascal's Triangle II

119. Pascal's Triangle II

119. Pascal's Triangle II@python

119. Pascal's Triangle II

119. Pascal's Triangle II

119. Pascal's Triangle II