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;
    }
};

Pascal's Triangle II - LeetCode

小结

相关文章:

  • 2021-12-19
  • 2022-01-12
  • 2022-12-23
  • 2021-04-28
  • 2021-04-17
  • 2022-01-18
  • 2021-06-22
猜你喜欢
  • 2022-12-23
  • 2022-01-27
  • 2022-02-23
  • 2022-02-26
  • 2021-08-12
  • 2021-10-07
  • 2022-12-23
相关资源
相似解决方案