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;
    }
};
相关推荐
念恒1230626 分钟前
网络基础
linux·网络·c++
AI小码27 分钟前
把动作「画」给视频世界模型,跨本体双向推演,李飞飞参与
大数据·人工智能·算法·ai·大模型·音视频·编程
mct1231 小时前
c++ iconv 字符utf-8转换gb2312失败
开发语言·c++
ziguo11221 小时前
Windows API 文件操作超级指南——从入门到内核
c++·windows·visualstudio
KaMeidebaby2 小时前
卡梅德生物技术快报|bli亲和力检测gst:告别批量跑胶:BLI实时酶切监测技术加速GST融合蛋白下游流程优化
前端·网络·数据库·人工智能·算法
alphaTao2 小时前
LeetCode 每日一题 2026/7/20-2026/7/26
算法·leetcode
zhangfeng11333 小时前
Ascendc 大语言模型框架 AscendCraft 和pypto 的区别 pypto生成算子c++源码吗
c++·人工智能·语言模型·算子开发
一只小灿灿3 小时前
C++ 修饰符全面详解
开发语言·c++
圣保罗的大教堂3 小时前
leetcode 3514. 不同 XOR 三元组的数目 II 中等
leetcode
code_pgf3 小时前
C/C++ 中 `typedef` 关键字详解
c语言·c++