LeetCode119. Pascal‘s Triangle II

文章目录

一、题目

Given an integer rowIndex, return the rowIndexth (0-indexed) row of the Pascal's triangle.

In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:

Example 1:

Input: rowIndex = 3

Output: [1,3,3,1]

Example 2:

Input: rowIndex = 0

Output: [1]

Example 3:

Input: rowIndex = 1

Output: [1,1]

Constraints:

0 <= rowIndex <= 33

Follow up: Could you optimize your algorithm to use only O(rowIndex) extra space?

二、题解

cpp 复制代码
class Solution {
public:
    vector<int> getRow(int rowIndex) {
        vector<int> before(1,1);
        if(rowIndex == 0) return before;
        int row = 1;
        while(rowIndex--){
            vector<int> res(row + 1,1);
            for(int i = 1;i < row;i++) res[i] = before[i-1] + before[i];
            before = res;
            row++;
        }
        return before;
    }
};
相关推荐
自我意识的多元宇宙6 小时前
树与二叉树--二叉树的存储结构
数据结构
BestOrNothing_20157 小时前
C++零基础到工程实战(4.3.3):vector数组访问与遍历
c++·迭代器·stl·vector·动态数组
charlie1145141917 小时前
通用GUI编程技术——图形渲染实战(三十三)——Direct2D与Win32/GDI互操作:渐进迁移实战
c++·图形渲染·gui·win32
文祐7 小时前
C++类之虚函数表及其内存布局(一个子类继承一个父类)
开发语言·c++
白羊by7 小时前
YOLOv1~v11 全版本核心演进总览
深度学习·算法·yolo
墨尘笔尖9 小时前
最大最小值降采样算法的优化
c++·算法
自我意识的多元宇宙10 小时前
二叉树的遍历和线索二叉树--二叉树的遍历
数据结构
YIN_尹10 小时前
【Linux系统编程】进程地址空间
linux·c++
qq_50242899010 小时前
清华大学与微软亚洲研究院出品:Kronos 本地部署教程
数据结构·python·金融量化·kronos开源模型
EverestVIP10 小时前
C++中空类通常大小为1的原理
c++