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;
    }
};
相关推荐
Mem0rin8 分钟前
[Java/数据结构]线性表之链表
java·数据结构·链表
kyle~31 分钟前
ROS2 --- WaitSet(等待集) 等待实体就绪,管理执行回调函数
大数据·c++·机器人·ros2
人大博士的交易之路39 分钟前
数据结构算法——python数据结构
开发语言·数据结构·python
量子炒饭大师1 小时前
【C++进阶】Cyber骇客的赛博血统上传——【面向对象之 继承 】一文带你搞懂面向对象编程的三要素之————继承
c++·dubbo·继承·面向对象编程
Tanecious.1 小时前
蓝桥杯备赛:Day2-B3612 求区间和
c++·蓝桥杯
C+++Python1 小时前
Linux/C++多进程
linux·运维·c++
stolentime1 小时前
通信题:洛谷P15942 [JOI Final 2026] 赌场 / Casino题解
c++·算法·洛谷·joi·通信题
XZHOUMIN1 小时前
【生成pdf格式的报告】
c++·pdf·mfc
初生牛犊不怕苦1 小时前
与AI一起学习《C专家编程》:数组与指针
c语言·学习·算法
elseif1231 小时前
浅谈 C++ 学习
开发语言·c++·学习