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;
    }
};
相关推荐
..过云雨3 分钟前
【负载均衡oj项目】01. 项目概述及准备工作
linux·c++·html·json·负载均衡
郝学胜-神的一滴6 分钟前
深度解析:Python元类手撸ORM框架,解锁底层编程魔法
数据结构·数据库·python·算法·职场和发展
yuuki2332337 分钟前
【C++ 智能指针全解析】从内存泄漏痛点到 RAII + unique/shared/weak_ptr 手撕实现
开发语言·c++
big_rabbit050212 分钟前
[算法][力扣219]存在重复元素2
数据结构·算法·leetcode
闻缺陷则喜何志丹19 分钟前
【构造 前缀和】P8902 [USACO22DEC] Range Reconstruction S|普及+
c++·算法·前缀和·洛谷·构造
摸鱼仙人~21 分钟前
动态规划求解 20 个通用模板
算法·动态规划
记忆多26 分钟前
c++内联函数
算法
仟濹39 分钟前
【算法打卡day20(2026-03-12 周四)算法/技巧:哈希表,双指针,字符串交换处理】5个题
数据结构·算法·散列表
老四啊laosi40 分钟前
[C++进阶] 16. 继承
c++·继承
陌夏40 分钟前
双指针与滑动窗口
算法