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;
    }
};
相关推荐
垆边人似月.2 分钟前
华为题:矩阵中的最长递增路径
算法·深度优先
小诗懂技术5 分钟前
linux驱动中断,相关中断的方式与适用场景
linux·arm开发·驱动开发·算法
不会就选b9 分钟前
算法日常・每日刷题--<动态规划>25
算法
fpcc12 分钟前
c++20新功能—std::osyncstream
c++·c++20
H.莓飛12 分钟前
【C++】模板初阶
linux·开发语言·c++·visual studio
十五年专注C++开发14 分钟前
基于Asio2实现http服务器的实践总结(二)
服务器·c++·http·asio2
技术不好的崎鸣同学18 分钟前
第七章:模块踩踏以覆写导出函数绕过CFG
c++·windows·安全·系统安全
Keep_Trying_Go18 分钟前
分割算法FastSAM论文详解
深度学习·算法·计算机视觉
miofly19 分钟前
GitHub 今日推荐|ps2-web-recomp:浏览器里跑 PS2 游戏的静态重编译方案
c++·开源·github
星夜离尘5619 分钟前
MATLAB 5 点滑动平均:filter 与 conv 的输出对齐,以及 2 样本延时的含义
开发语言·算法·matlab·滤波·fir