当前位置:网站首页>LeetCode 0119. 杨辉三角 II - 基于原地滚动的空间优化
LeetCode 0119. 杨辉三角 II - 基于原地滚动的空间优化
2022-07-19 02:46:00 【Tisfy】
【LetMeFly】119.杨辉三角 II:基于原地滚动的空间优化
力扣题目链接:https://leetcode.cn/problems/pascals-triangle-ii/
给定一个非负索引 rowIndex
,返回「杨辉三角」的第 rowIndex
行。
在「杨辉三角」中,每个数是它左上方和右上方的数的和。
示例 1:
输入: rowIndex = 3 输出: [1,3,3,1]
示例 2:
输入: rowIndex = 0 输出: [1]
示例 3:
输入: rowIndex = 1 输出: [1,1]
提示:
0 <= rowIndex <= 33
进阶:
你可以优化你的算法到 O(rowIndex)
空间复杂度吗?
方法一:构造整个杨辉三角
这道题和LeetCode 118.杨辉三角类似。
不同之处在于:
- 这道题只需要返回最后一行
- 这道题的行数是从0开始的
那么方法一就是类似LeetCode 118.杨辉三角,返回时只返回最后一行即可。
具体思路可参考https://leetcode.letmefly.xyz/2022/07/17/LeetCode 0118.杨辉三角/
- 时间复杂度 O ( N 2 ) O(N^2) O(N2)
- 空间复杂度 O ( N 2 ) O(N^2) O(N2),因为需要存储整个三角
AC代码
C++
class Solution {
public:
vector<int> getRow(int numRows) {
numRows++;
vector<vector<int>> ans;
ans.push_back({
1});
for (int i = 1; i < numRows; i++) {
ans.push_back({
1});
for (int j = 1; j < i; j++) {
ans[i].push_back(ans[i - 1][j - 1] + ans[i - 1][j]);
}
ans[i].push_back(1);
}
return ans.back();
}
};
方法二:原地滚动 + 只构造最后一行
不难发现,第 i + 1 i+1 i+1行的计算只需要用到第 i i i行的值。
因此,我们只开辟最后一行的空间,并且原地滚动即可。
原地滚动的时候,记得从后往前计算。
- 时间复杂度 O ( N 2 ) O(N^2) O(N2)
- 空间复杂度 O ( 1 ) O(1) O(1),答案不计入算法空间复杂度
AC代码
C++
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> ans(rowIndex + 1);
ans[0] = 1;
for (int row = 1; row <= rowIndex; row++) {
ans[row] = 1;
for (int th = row - 1; th > 0; th--) {
// 必须是从后往前
ans[th] += ans[th - 1];
}
}
return ans;
}
};
同步发文于CSDN,原创不易,转载请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/125853536
边栏推荐
- 天气预报仪触摸芯片-DLT8SA15A-杰力科创
- 17_收集表单数据
- 11.3 generation of permutations and combinations (no re set elements)
- MySQL详细知识点总结 可以收藏啦
- [wechat applet] curriculum case -- 0 basic version
- NPE: An FPGA - based overlay Processor for Natural Language
- E. Split Into Two Sets(二分图性质 + 种类并查集(扩展域))
- 【论文阅读】CoaT:Co-Scale Conv-Attentional Image Transformers
- 什么是跨站脚本 (XSS)?
- Why is there an unsafe prompt on the website when SSL certificate is installed?
猜你喜欢
P3166数三角形(容斥+gcd)
数据库压力测试方法概述
【论文阅读】CoaT:Co-Scale Conv-Attentional Image Transformers
2022爱分析・银行数字化实践报告
18_ filter
Live broadcast today | Apache pulsar meetup: vivo, Tencent cloud, bigo, Yunxing technology practice sharing
Redis删除策略和淘汰策略
硅谷课堂第八课-腾讯云点播管理模块(三)
E. Split Into Two Sets(二分图性质 + 种类并查集(扩展域))
[wechat applet] curriculum case -- 0 basic version
随机推荐
云笔记有什么功能作用,浏览器如何添加云笔记插件
PHP支付宝转账到支付宝账号
分享搭建脚手架的一些经验
简历里写了电商项目 ,面试的时候怎么回答
CodisVSRedisCluster:我该选择哪一个集群方案?
Enter the enterprise series | streamnational x Zhong'an insurance
Jedis
[haoi2012] volume adjustment (up to backpack)
2、登录——验证码功能以及保存登录状态
机器人工程的工作与考研之困惑“卷”补充
Typora 过期解决方法,Typora打不开了怎么办
Regression analysis model
09 design of water quality monitoring system based on ZigBee
D. Rating Compression(思维 + 双指针)
Special binary tree and exercises
我就是测试一下而已
Sovit3d rapid development of intelligent agriculture 3D visualization system of Internet of things
Mark and Lightbulbs(思维)
Fundamentals of C language: structure (elementary level)
【微信小程序】课程表案例--0基础版