杨辉三角 II

给定一个非负索引 rowIndex,返回「杨辉三角」的第 rowIndex行。

在「杨辉三角」中,每个数是它左上方和右上方的数的和。

示例 1:

复制代码
输入: rowIndex = 3
输出: [1,3,3,1]

示例 2:

复制代码
输入: rowIndex = 0
输出: [1]

示例 3:

复制代码
输入: rowIndex = 1
输出: [1,1]

提示:

  • 0 <= rowIndex <= 33

进阶:

你可以优化你的算法到 O (rowIndex) 空间复杂度吗?

java 复制代码
class Solution {
    public List<Integer> getRow(int rowIndex) {
        List<List<Integer>> list = new ArrayList<>();
        for (int i = 0; i <= rowIndex; i++) {
            List<Integer> temp = new ArrayList<>();
            if (i == 0) {
                temp.add(1);
            } else {
                List<Integer> last = list.get(i - 1);
                for (int j = 0; j < last.size(); j++) {
                    if (j == 0) {
                        temp.add(last.get(j));
                    }
                    if (j > 0) {
                        temp.add(last.get(j - 1) + last.get(j));
                    }
                    if (j == last.size() - 1) {
                        temp.add(last.get(last.size() - 1));
                    }
                }
            }
            list.add(temp);
        }
        return list.get(list.size() - 1);
    }
}
相关推荐
阿崽meitoufa5 分钟前
JVM虚拟机:垃圾收集器和判断对象是否存活的算法
java·jvm·算法
ballball~~1 小时前
拉普拉斯金字塔
算法·机器学习
Cemtery1161 小时前
Day26 常见的降维算法
人工智能·python·算法·机器学习
Ethan-D2 小时前
#每日一题19 回溯 + 全排列思想
java·开发语言·python·算法·leetcode
Benny_Tang2 小时前
题解:CF2164C Dungeon
c++·算法
仙俊红2 小时前
LeetCode174双周赛T3
数据结构·算法
橘颂TA3 小时前
【剑斩OFFER】算法的暴力美学——LeetCode 733 题:图像渲染
算法·leetcode·职场和发展
不穿格子的程序员3 小时前
从零开始写算法——回溯篇2:电话号码的字母组合 + 组合总和
算法·深度优先·回溯
持梦远方4 小时前
算法剖析1:摩尔投票算法 ——寻找出现次数超过一半的数
c++·算法·摩尔投票算法
程序员-King.4 小时前
链表——算法总结与新手教学指南
数据结构·算法·链表