Leetcode 119. 杨辉三角 II

主要思路:先将杨辉三角的二维数组预处理出来,再根据题目要求,求出rowIndex行。

Python:

python 复制代码
class Solution:
    def getRow(self, rowIndex: int) -> List[int]:
        yh_trigle = [[0] * 34 for _ in range(34)]
        for i in range(34):
            yh_trigle[i][i] = 1
            yh_trigle[i][0] = 1
            for j in range(1, i):
                yh_trigle[i][j] = yh_trigle[i - 1][j] + yh_trigle[i - 1][j - 1]
        return yh_trigle[rowIndex][0: rowIndex + 1]

C++:(自己写的老出现越界的情况,实在不明白!!!,以下参考了灵神的代码,但是我的思路是一样的)

cpp 复制代码
const int MX = 34;
vector<int> c[MX];

auto init = []() {
    for (int i = 0; i < MX; i++) {
        c[i].resize(i + 1, 1);
        for (int j = 1; j < i; j++) {
            // 左上方的数 + 正上方的数
            c[i][j] = c[i - 1][j - 1] + c[i - 1][j];
        }
    }
    return 0;
}();

class Solution {
public:
    vector<int> getRow(int rowIndex) {
        return c[rowIndex];
    }
};

加油!!!

相关推荐
江沉晚呤时6 小时前
在 C# 中调用 Python 脚本:实现跨语言功能集成
python·microsoft·c#·.net·.netcore·.net core
m0_535064607 小时前
C++模版编程:类模版与继承
java·jvm·c++
今天背单词了吗9807 小时前
算法学习笔记:19.牛顿迭代法——从原理到实战,涵盖 LeetCode 与考研 408 例题
笔记·学习·算法·牛顿迭代法
电脑能手7 小时前
如何远程访问在WSL运行的Jupyter Notebook
ide·python·jupyter
Edward-tan7 小时前
CCPD 车牌数据集提取标注,并转为标准 YOLO 格式
python
老胖闲聊8 小时前
Python I/O 库【输入输出】全面详解
开发语言·python
jdlxx_dongfangxing8 小时前
进制转换算法详解及应用
算法
倔强青铜三8 小时前
苦练Python第18天:Python异常处理锦囊
人工智能·python·面试
Tanecious.8 小时前
C++--红黑树封装实现set和map
网络·c++
倔强青铜三8 小时前
苦练Python第17天:你必须掌握的Python内置函数
人工智能·python·面试