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;
    }
};
相关推荐
黎阳之光1 分钟前
黎阳之光:数智硬核技术赋能应急管理装备创新,筑牢安全防线
大数据·人工智能·科技·算法·安全
Qt程序员6 分钟前
深入理解 Linux 内核 RCU 机制:从原理到实现
linux·c++·内核·linux内核·rcu
进击的小头7 分钟前
第19篇:卡尔曼滤波器与MPC模型预测控制器的结合实战
python·算法
吴梓穆8 分钟前
UE5 c++打印日志
开发语言·c++·ue5
吴梓穆9 分钟前
UE5 C++ 绘制图形调试宏
开发语言·c++·ue5
2501_9083298510 分钟前
C++中的装饰器模式
开发语言·c++·算法
x***r15111 分钟前
Dev C++ 6.5安装与配置教程 Windows版:解压+管理员运行+自定义路径+中文设置指南
开发语言·c++
2301_7887705511 分钟前
OJ模拟2
数据结构·算法
Q741_14717 分钟前
每日一题 力扣 3548. 等和矩阵分割 II 前缀和 哈希表 C++ 题解
算法·leetcode·前缀和·矩阵·力扣·哈希表
木井巳18 分钟前
【递归算法】全排列 Ⅱ
java·算法·leetcode·决策树·深度优先·剪枝