Pascal's Triangle II

题目链接

Pascal's Triangle II - LeetCode

注意点

  • 只能使用O(k)的额外空间
  • 有可能numRows等于0

解法

解法一:除了第一个数为1之外,后面的数都是上一次循环的数值加上它前面位置的数值之和,不停地更新每一个位置的值,便可以得到第n行的数字。

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

小结

原文地址:https://www.cnblogs.com/multhree/p/10626710.html