119. 杨辉三角 II(Java)

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

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

示例 1:

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

示例 2:

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

示例 3:

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

提示:

  • 0 <= rowIndex <= 33

解法:

在杨辉三角的基础上改动:

java 复制代码
class Solution {
    public List<Integer> getRow(int rowIndex) {
        List<List<Integer>> listList = new ArrayList<>();
        int row = 1;
        while (row <= rowIndex + 1) {
            //生成行
            List<Integer> list = new ArrayList<>();
            for (int i = 0; i < row; i++) {
                if (i == 0 || i == row - 1) {
                    list.add(1);
                } else {
                    List<Integer> sRow = listList.get(row - 2);
                    Integer f = sRow.get(i) + sRow.get(i - 1);
                    list.add(f);
                }
            }
            listList.add(list);
            row++;
        }
        return listList.get(rowIndex);
    }
}
相关推荐
地平线开发者4 小时前
J6B vio scenario sample
算法
Flittly11 小时前
【AgentScope Java新手村系列】(16)从RAG到多路检索
java·spring boot·spring
小兔崽子去哪了12 小时前
Java 生成二维码解决方案
java·后端
BothSavage16 小时前
Trae远程开发中DeepSeek自定义模型4054错误的排查与修复
算法
小林ixn16 小时前
从暴力到KMP:一道题彻底搞懂字符串匹配的前世今生
算法
人活一口气16 小时前
从JVM调优到MCP协议:Java全栈技术体系深度总结与企业级架构实践
java·spring boot
烬羽18 小时前
字符串算法入门:从反转字符串到回文判断,面试不再慌
算法·面试
NE_STOP18 小时前
Vibe Coding -- 完整项目案例实操
java
荣码18 小时前
GraphRAG:普通RAG只能回答"点"的问题,我踩了4个坑才搞懂
java·python