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;
    }
};
相关推荐
liulilittle6 小时前
FileStream C++
开发语言·c++·cocoa
Gomiko6 小时前
C/C++基础(五):分支
c语言·c++
wearegogog1236 小时前
光谱分析波段选择的连续投影算法
算法
点PY6 小时前
C++ 中 std::async 和 std::future 的并发性
java·开发语言·c++
执笔论英雄6 小时前
【RL】DAPO 数据处理
算法
不会代码的小猴6 小时前
C++的第九天笔记
开发语言·c++·笔记
why1517 小时前
面经整理——算法
java·数据结构·算法
悦悦子a啊7 小时前
将学生管理系统改造为C/S模式 - 开发过程报告
java·开发语言·算法
痕忆丶7 小时前
双线性插值缩放算法详解
算法
fqbqrr7 小时前
2512C++,clangd支持模块
开发语言·c++