problem
code
class Solution { public: vector<int> getRow(int rowIndex) { vector<int> res(rowIndex+1, 1); if(rowIndex<2) return res;// for(int i=2; i<=rowIndex; i++)//row. { for(int j=i-1; j>=0; j--)// { res[j] = res[j] + res[j-1]; } } return res; } };
当我们需要改变数组的值时,如果从前往后遍历,有时会带来很多麻烦,比如需要插入值,导致数组平移,或者新的值覆盖了旧有的值,但旧有的值依然需要被使用。这种情况下,有时仅仅改变一下数组的遍历方向,就会避免这些困难。
class Solution { public: vector<int> getRow(int rowIndex) { vector<int> res(rowIndex+1, 0); res[0] = 1; for(int i=1; i<=rowIndex; i++)//row. { for(int j=i; j>=0; j--)// { res[j] = res[j] + res[j-1]; } } return res; } };
参考
1. Leetcode_Pascal's Triangle II;
2. felix_cnblogs;
3. https://www.cnblogs.com/ccsccs/articles/4214260.html;
完