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;
    }
};
相关推荐
卷福同学1 小时前
【养虾日记】Openclaw操作浏览器自动化发文
人工智能·后端·算法
春日见1 小时前
如何入门端到端自动驾驶?
linux·人工智能·算法·机器学习·自动驾驶
㓗冽2 小时前
分解质因数-进阶题10
c++
图图的点云库2 小时前
高斯滤波实现算法
c++·算法·最小二乘法
一叶落4383 小时前
题目:15. 三数之和
c语言·数据结构·算法·leetcode
y = xⁿ3 小时前
【LeetCodehot100】2:两数相加 19 删除链表倒数第n个节点
数据结构·链表
CoderCodingNo3 小时前
【GESP】C++七级考试大纲知识点梳理, (1) 数学库常用函数
开发语言·c++
老鱼说AI3 小时前
CUDA架构与高性能程序设计:异构数据并行计算
开发语言·c++·人工智能·算法·架构·cuda
罗湖老棍子4 小时前
【例 1】数列操作(信息学奥赛一本通- P1535)
数据结构·算法·树状数组·单点修改 区间查询
big_rabbit05024 小时前
[算法][力扣222]完全二叉树的节点个数
数据结构·算法·leetcode