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;
    }
};
相关推荐
AI柠檬26 分钟前
机器学习:数据集的划分
人工智能·算法·机器学习
_OP_CHEN33 分钟前
C++进阶:(四)set系列容器的全面指南
开发语言·c++·stl·set·multiset·关联式容器·setoj题
代码雕刻家39 分钟前
1.4.课设实验-数据结构-单链表-文教文化用品品牌2.0
c语言·数据结构
让我们一起加油好吗1 小时前
【数论】裴蜀定理与扩展欧几里得算法 (exgcd)
算法·数论·裴蜀定理·扩展欧几里得算法·逆元
Geo_V1 小时前
提示词工程
人工智能·python·算法·ai
云边有个稻草人1 小时前
Rust 借用分割技巧:安全解构复杂数据结构
数据结构·安全·rust
侯小啾1 小时前
【22】C语言 - 二维数组详解
c语言·数据结构·算法
qq_479875431 小时前
Linux time function in C/C++【2】
linux·c语言·c++
TL滕1 小时前
从0开始学算法——第一天(如何高效学习算法)
数据结构·笔记·学习·算法
傻童:CPU1 小时前
DFS迷宫问题
算法·深度优先