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;
    }
};
相关推荐
来荔枝一大筐13 分钟前
力扣 寻找两个正序数组的中位数
算法
算法与编程之美24 分钟前
理解Java finalize函数
java·开发语言·jvm·算法
怕什么真理无穷30 分钟前
C++面试4-线程同步
java·c++·面试
学编程就要猛38 分钟前
数据结构初阶:Java中的Stack和Queue
数据结构
地平线开发者1 小时前
LLM 训练基础概念与流程简介
算法·自动驾驶
是苏浙1 小时前
零基础入门C语言之C语言实现数据结构之顺序表
c语言·开发语言·数据结构
点云SLAM1 小时前
弱纹理图像特征匹配算法推荐汇总
人工智能·深度学习·算法·计算机视觉·机器人·slam·弱纹理图像特征匹配
星释1 小时前
Rust 练习册 :Matching Brackets与栈数据结构
数据结构·算法·rust
地平线开发者1 小时前
Camsys 时间戳信息简介
算法·自动驾驶
星释1 小时前
Rust 练习册 :Luhn与校验算法
java·算法·rust