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;
    }
};
相关推荐
励志的小陈14 分钟前
双指针算法--移除元素、删除有序数组中的重复项、合并两个有序数组
算法
Summer_Uncle25 分钟前
【QT学习】Qt界面布局的生命周期和加载时机
c++·qt
小CC吃豆子34 分钟前
C++ 继承
开发语言·c++
hoiii18739 分钟前
Mean Shift目标跟踪算法MATLAB实现
算法·matlab·目标跟踪
alphaTao39 分钟前
LeetCode 每日一题 2026/3/23-2026/3/29
服务器·windows·leetcode
励志的小陈39 分钟前
复杂度算法题——旋转数组(三种思路)
c语言·数据结构·算法
tankeven40 分钟前
HJ151 模意义下最大子序列和(Easy Version)
c++·算法
Sirens.1 小时前
对顺序表以及双向链表的理解
数据结构·链表
fengenrong1 小时前
20260325
开发语言·c++
BestOrNothing_20151 小时前
从C++结构体、类到 PID 控制器:运动控制初学者如何理解 C++ 工程代码
c++·面向对象·pid·运动控制·.h与.cpp·struct与class