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;
    }
};
相关推荐
风中的微尘2 小时前
39.网络流入门
开发语言·网络·c++·算法
混分巨兽龙某某3 小时前
基于Qt Creator的Serial Port串口调试助手项目(代码开源)
c++·qt creator·串口助手·serial port
西红柿维生素3 小时前
JVM相关总结
java·jvm·算法
小冯记录编程3 小时前
C++指针陷阱:高效背后的致命危险
开发语言·c++·visual studio
C_Liu_4 小时前
C++:类和对象(下)
开发语言·c++
coderxiaohan4 小时前
【C++】类和对象1
java·开发语言·c++
阿昭L4 小时前
MFC仿真
c++·mfc
ChillJavaGuy5 小时前
常见限流算法详解与对比
java·算法·限流算法
散1125 小时前
01数据结构-01背包问题
数据结构
sali-tec5 小时前
C# 基于halcon的视觉工作流-章34-环状测量
开发语言·图像处理·算法·计算机视觉·c#