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;
    }
};
相关推荐
1000世界小札3 小时前
《大话数据结构》第9章精读:归并排序与快速排序完整 C++ 实现
数据结构·c++·算法
2601_956121976 小时前
背包基础篇(01、完全、分组、多重、混合)
c++·算法·动态规划
fpcc6 小时前
跟我学C++中级篇——编译期的条件选择
开发语言·c++
兴通物联科技7 小时前
SMT PCB 微小 DataMatrix 码扫不动问题分析 兴通 XT8601B 600 万像素工业读码器落地实践
大数据·人工智能·单片机·嵌入式硬件·算法·计算机视觉
月华路9 小时前
G1 GC 对数组与大对象(Humongous)的处理
java·jvm·算法
我想走路带风9 小时前
LRU和最长前缀和(计算机网络算法)
计算机网络·算法
M78佐菲10 小时前
Linux学习笔记:进程
linux·笔记·学习·算法
ShineWinsu10 小时前
对于C++:C++20中线程、初始化、Lambda与内存视图等特性的解析
c++·c++20
徐小夕11 小时前
表格、文档、甘特、大屏、表单一站打通:pxcharts超级表格4.0正式上线!
前端·算法·github