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;
    }
};
相关推荐
mjhcsp2 分钟前
AT_arc205_c [ARC205C] No Collision Moves 题解
开发语言·c++·算法·题解
Yzzz-F2 分钟前
Problem - 1114C - Codeforces[勒让德公式]
算法
木井巳18 分钟前
【笔试强训】Day1
java·算法
leiming619 分钟前
巧用 FreeRTOS 任务通知作“邮箱”:NeoPixel 灯环控制实战
java·前端·算法
老四啊laosi19 分钟前
[双指针] 4. 力扣--盛最多水的容器
算法·leetcode·装水最多的容器
wanderist.21 分钟前
高维矩阵的压维存储和高维差分
c++·算法·蓝桥杯
迈巴赫车主29 分钟前
蓝桥杯192.等差数列java
java·数据结构·算法·职场和发展·蓝桥杯
chase。39 分钟前
【学习笔记】从经典算法到通用神经运动规划器
笔记·学习·算法
王璐WL43 分钟前
【C++】经典且易错的题
c++
feasibility.1 小时前
OpenCV图像滤波算法应用:常见滤波器的原理与效果对比(含c++/python代码与中文显示)
c++·opencv·算法