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;
    }
};
相关推荐
NGC_66114 分钟前
插入排序算法
java·数据结构·算法
bubiyoushang8887 分钟前
基于遗传算法的LQR控制器最优设计算法
开发语言·算法·matlab
每天要多喝水9 分钟前
图论Day39:孤岛题目
算法·深度优先·图论
mingren_131414 分钟前
SDL3配置及基本使用(完整demo)
开发语言·c++·音视频
兩尛15 分钟前
648. 单词替换
算法
sycmancia16 分钟前
C++——完善的复数类
开发语言·c++
廋到被风吹走16 分钟前
稳定性保障:限流降级深度解析 —— Sentinel滑动窗口算法与令牌桶实现
运维·算法·sentinel
MicroTech202518 分钟前
MLGO微算法科技利用开放量子系统,Lindbladian 模拟驱动的新一代量子微分方程算法亮相
科技·算法·量子计算
无限进步_20 分钟前
138. 随机链表的复制 - 题解与详细分析
c语言·开发语言·数据结构·算法·链表·github·visual studio