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?

二、题解

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. Leetcode 169

    2024-01-09 01:08:01       43 阅读
  2. [leetcode] 169. 多数元素

    2024-01-09 01:08:01       54 阅读
  3. Leetcode 189. 轮转数组

    2024-01-09 01:08:01       44 阅读
  4. leetcode112.路径总和

    2024-01-09 01:08:01       39 阅读
  5. LeetCode112 路径总和

    2024-01-09 01:08:01       41 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-09 01:08:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-09 01:08:01       101 阅读
  3. 在Django里面运行非项目文件

    2024-01-09 01:08:01       82 阅读
  4. Python语言-面向对象

    2024-01-09 01:08:01       91 阅读

热门阅读

  1. 问答:攻击面发现及管理

    2024-01-09 01:08:01       63 阅读
  2. 如何获取时间戳?

    2024-01-09 01:08:01       60 阅读
  3. Android Framework默认授予第三方APP悬浮窗权限

    2024-01-09 01:08:01       57 阅读
  4. 问答:攻击面发现及管理

    2024-01-09 01:08:01       63 阅读
  5. MySQL5.7 InnoDB 磁盘结构之索引Index

    2024-01-09 01:08:01       59 阅读
  6. C++面对对象编程进阶(2)

    2024-01-09 01:08:01       53 阅读
  7. Arrays 的使用

    2024-01-09 01:08:01       54 阅读
  8. c++ execl 执行 重定向

    2024-01-09 01:08:01       51 阅读