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;
    }
};
相关推荐
沐苏瑶27 分钟前
Java 搜索型数据结构全解:二叉搜索树、Map/Set 体系与哈希表
java·数据结构·算法
ccLianLian43 分钟前
深度学习·DDPM
数据结构
ZoeJoy81 小时前
算法筑基(二):搜索算法——从线性查找到图搜索,精准定位数据
算法·哈希算法·图搜索算法
Alicx.2 小时前
dfs由易到难
算法·蓝桥杯·宽度优先
桦02 小时前
【C++复习】:继承
开发语言·c++
_日拱一卒2 小时前
LeetCode:找到字符串中的所有字母异位词
算法·leetcode
鱼难终2 小时前
类和对象(下)
c++
云泽8082 小时前
深入 AVL 树:原理剖析、旋转算法与性能评估
数据结构·c++·算法
Wilber的技术分享3 小时前
【LeetCode高频手撕题 2】面试中常见的手撕算法题(小红书)
笔记·算法·leetcode·面试
邪神与厨二病3 小时前
Problem L. ZZUPC
c++·数学·算法·前缀和