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;
    }
};
相关推荐
WitsMakeMen几秒前
RankMixer论文理解
算法
思茂信息5 分钟前
基于 CST 的方向图可重构天线仿真分析
网络·人工智能·单片机·算法·重构·cst·电磁仿真
IronMurphy5 分钟前
【算法三十三】17. 电话号码的字母组合
算法
菜菜小狗的学习笔记7 分钟前
剑指Offer算法题(十)排序
数据结构
逆境不可逃8 分钟前
LeetCode 热题 100 之 131. 分割回文串 51. N 皇后
算法·leetcode·职场和发展
进击的小头9 分钟前
第21篇:BUCK变换器双环控制系统设计与参数整定调试实战
python·算法
feng_you_ying_li10 分钟前
set/map的封装,底层为红黑树.
c++
晨非辰10 分钟前
Git版本控制速成:提交三板斧/日志透视/远程同步15分钟精通,掌握历史回溯与多人协作安全模型
linux·运维·服务器·c++·人工智能·git·后端
liliangcsdn10 分钟前
信息检索评估指标Recall@K的分析和计算示例
算法·全文检索
gdizcm11 分钟前
linux判断文件类型的多种方法
linux·c++