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;
    }
};
相关推荐
wljy13 分钟前
第十三届蓝桥杯大赛软件赛省赛C/C++ 大学 B 组(个人见解,已完结)
c语言·c++·算法·蓝桥杯·stl
高一要励志成为佬20 分钟前
【数据结构】算法复杂度
数据结构
清空mega1 小时前
C++中关于数学的一些语法回忆(2)
开发语言·c++·算法
想唱rap1 小时前
线程池以及读写问题
服务器·数据库·c++·mysql·ubuntu
香蕉鼠片1 小时前
数据结构八股(一)
数据结构·算法
起个破名想半天了1 小时前
算法与数据结构之排序
数据结构·排序算法·排序·算法与数据结构
Mr_Xuhhh1 小时前
从理论到实践:深入理解算法的时间与空间复杂度
java·开发语言·算法
望眼欲穿的程序猿1 小时前
Vscode Clangd 无法索引 C++17 或者以上标准
java·c++·vscode
6Hzlia2 小时前
【Hot 100 刷题计划】 LeetCode 42. 接雨水 | C++ 动态规划与双指针题解
c++·算法·leetcode
地平线开发者2 小时前
智能驾驶感知算法的演进
算法·自动驾驶