Leetcode No.119 Pascal's Triangle II(c++实现)
1. 题目
1.1 英文题目
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:
1.2 中文题目
输出杨辉三角形的指定行
1.3输入输出
| 输入 | 输出 |
|---|---|
| rowIndex = 3 | [1,3,3,1] |
| rowIndex = 0 | [1] |
| rowIndex = 1 | [1,1] |
1.4 约束条件
0 <= rowIndex <= 33
2. 实验平台
IDE:VS2019
IDE版本:16.10.1
语言:c++11
3. 分析
这一题最简单粗暴的方法就是先求出到指定行的杨辉三角形,之后再取最后一行作为结果,代码为:
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<vector<int>> ans(rowIndex + 1);
for(int i = 0; i < rowIndex + 1; i++)
{
ans[i].resize(i + 1);
ans[i][0] = ans[i][i] = 1;
for(int j = 1; j < i; j++)
{
ans[i][j] = ans[i - 1][j - 1] + ans[i - 1][j];
}
}
return ans[rowIndex];
}
};
这样做也固然没问题,但是算法很冗杂,不够优化,我们可以适当优化下,不需要把所有行的结果都存储起来,只需要保存最后一行。新代码如下:
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> ans;
for(int i = 0; i < rowIndex + 1; i++)
{
vector<int> temp(i + 1);
temp[0] = temp[i] = 1;
for(int j = 1; j < i; j++)
{
temp[j] = ans[j - 1] + ans[j];
}
ans = temp;
}
return ans;
}
};
但是我们提交后发现算法时间和空间复杂度都没变,于是我在想还有没有优化空间,我发现每行计算时都需要重新定义temp,并为其开辟内存空间,有待优化,故可以将其提前定义,并在每行计算时重定义temp大小,代码如下:
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> ans;
vector<int> temp;
for(int i = 0; i < rowIndex + 1; i++)
{
temp.resize(i + 1);
temp[0] = temp[i] = 1;
for(int j = 1; j < i; j++)
{
temp[j] = ans[j - 1] + ans[j];
}
ans = temp;
}
return ans;
}
};
这次性能不错。但是我觉得有个temp,还是很繁琐,那么能不能去掉temp呢,但是如果去掉temp,递推那一步就会涉及混乱,考虑到递推关系式是j-1和j,于是我们可以在递推时进行反向递推,代码如下:
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> ans;
for(int i = 0; i < rowIndex + 1; i++)
{
ans.resize(i + 1);
ans[0] = ans[i] = 1;
for(int j = i - 1; j > 0; j--)
ans[j] += ans[j - 1];
}
return ans;
}
};
这次算法空间复杂度又提高了,另外,每次都要重新定义ans的尺寸,能不能不这么做呢?我想到每次循环只是比之前尺寸多1,因此可以不重新定义尺寸,而是尺寸加1,即使用push_back();具体代码如下:
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> ans;
for(int i = 0; i < rowIndex + 1; i++)
{
ans.push_back(1);
for(int j = i - 1; j > 0; j--)
ans[j] += ans[j - 1];
}
return ans;
}
};
Leetcode No.119 Pascal's Triangle II(c++实现)的更多相关文章
- LeetCode OJ 119. Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...
- 【一天一道LeetCode】#119. Pascal's Triangle II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】119. Pascal's Triangle II
题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [ ...
- leetcode 118. Pascal's Triangle 、119. Pascal's Triangle II 、120. Triangle
118. Pascal's Triangle 第一种解法:比较麻烦 https://leetcode.com/problems/pascals-triangle/discuss/166279/cpp- ...
- 118/119. Pascal's Triangle/II
原文题目: 118. Pascal's Triangle 119. Pascal's Triangle II 读题: 杨辉三角问题 '''118''' class Solution(object): ...
- 119. Pascal's Triangle II@python
Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note t ...
- [LeetCode] 119. Pascal's Triangle II 杨辉三角之二
Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note t ...
- [LeetCode]题解(python):119 Pascal's Triangle II
题目来源 https://leetcode.com/problems/pascals-triangle-ii/ Given an index k, return the kth row of the ...
- LeetCode 119. Pascal's Triangle II (杨辉三角之二)
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...
随机推荐
- 路由信息相关 route 网卡
目录 route命令 1.查看路由表 2.管理路由 基本网络配置 添加网卡地址 修改网卡UUID route命令 路由表管理命令,路由表主要构成: Destination: 目标网络ID,表示可以到达 ...
- Python中if name == 'main':的作用
一个python文件通常有两种使用方法, 第一是作为脚本直接执行. 第二是 import 到其它的 python 脚 本中被调用(模块重用)执行. 因此 if name == 'main': 的作用就 ...
- python实战项目练习-Django商城项目之注册功能实现
设计到的前端知识 项目的前端页面使用vue来实现局部刷新,通过数据的双向绑定实现与用户的交互,下面来看一下需求,在用户输入内容后,前端需要做一些简单的规则校验,我们希望在在用户输入后能够实时检测,如果 ...
- 工作流Activiti框架中表单的使用!详细解析内置表单和外置表单的渲染
Activiti中的表单 Activiti提供了一种方便而且灵活的方式在业务流程中以手工方式添加表单 对表单的支持有2种方式: 通过表单属性对内置表单进行渲染 通过表单属性对外置表单进行渲染 表单属性 ...
- Go语言网络通信---tcp群发消息
server package main import ( "fmt" "net" "os" "time" ) func ...
- 目标检测中特征融合技术(YOLO v4)(下)
目标检测中特征融合技术(YOLO v4)(下) ASFF:自适应特征融合方式 ASFF来自论文:<Learning Spatial Fusion for Single-Shot Object D ...
- 基于OpenSeq2Seq的NLP与语音识别混合精度训练
基于OpenSeq2Seq的NLP与语音识别混合精度训练 Mixed Precision Training for NLP and Speech Recognition with OpenSeq2Se ...
- runtime系统的Cello
runtime系统的Cello 通过充当一个现代的.功能强大的runtime系统,Cello使许多以前在C中不切实际或笨拙的事情变得简单,例如: 通用数据结构 多态函数 接口/类型类 构造函数/析构函 ...
- GPU端到端目标检测YOLOV3全过程(上)
GPU端到端目标检测YOLOV3全过程(上) Basic Parameters: Video: mp4, webM, avi Picture: jpg, png, gif, bmp Text: doc ...
- Spring Cloud09: Config 配置中心
一.概述 什么是配置中心呢,在基于微服务的分布式系统中,每个业务模块都可以拆分成独立自主的服务,由多个请求来协助完成某个需求,那么在某一具体的业务场景中,某一个请求需要调用多个服务来完成,那么就存在一 ...