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;
    }
};
相关推荐
狐璃同学13 小时前
数据结构(2)线性表
数据结构·算法
啦啦啦_999913 小时前
4. KNN算法之 特征预处理(归一化&标准化)
算法
AI是这个时代的魔法13 小时前
Unpack Nested Data:照亮你的数据结构
数据结构·python
淘气包海鸟14 小时前
雷达基本原理
算法·信息与通信
Tisfy14 小时前
LeetCode 2615.等值距离和:分组(哈希表+前缀和)
算法·leetcode·散列表
小此方14 小时前
Re:从零开始的 C++ 进阶篇(四)工业级 C++ 编程:如何构建异常安全的健壮系统?(含案例分析)
运维·开发语言·c++·安全
电商API_1800790524714 小时前
如何实现批量化自动化获取淘宝商品详情数据?爬虫orAPI?
大数据·c++·爬虫·自动化
t***54414 小时前
如何确认 Clang 是否在 Dev-C++ 中成功应用
java·开发语言·c++
啦啦啦_999914 小时前
2. KNN算法之 分类&回归API实现
算法
X journey14 小时前
机器学习进阶(23):K-means聚类
人工智能·算法·机器学习