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;
    }
};
相关推荐
yaoh.wang18 小时前
力扣(LeetCode) 119: 杨辉三角 II - 解法思路
数据结构·python·算法·leetcode·面试·职场和发展·跳槽
客梦18 小时前
数据结构--最小生成树
数据结构·笔记
CSDN_RTKLIB18 小时前
【类定义系列一】C++ 头文件 / 源文件分离
开发语言·c++
CoderCodingNo18 小时前
【GESP】C++五级真题(埃氏筛思想考点) luogu-B3929 [GESP202312 五级] 小杨的幸运数
数据结构·c++·算法
charlee4419 小时前
C++中JSON序列化和反序列化的实现
c++·json·序列化·结构体·nlohmann/json
机器学习之心19 小时前
基于PSO-GA混合算法的施工进度计划多目标优化,以最小化总成本并实现资源均衡,满足工期约束和资源限制,MATLAB代码
算法·matlab·多目标优化·pso-ga混合算法
bbq粉刷匠19 小时前
Java--二叉树概念及其基础应用
java·数据结构·算法
CodeByV19 小时前
【算法题】前缀和
算法
挖矿大亨19 小时前
c++中值传递时是如何触发拷贝构造函数的
开发语言·c++
高洁0119 小时前
知识图谱构建
人工智能·深度学习·算法·机器学习·知识图谱