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++实现)的更多相关文章

  1. 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, ...

  2. 【一天一道LeetCode】#119. Pascal's Triangle II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  3. 【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 [ ...

  4. 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- ...

  5. 118/119. Pascal's Triangle/II

    原文题目: 118. Pascal's Triangle 119. Pascal's Triangle II 读题: 杨辉三角问题 '''118''' class Solution(object): ...

  6. 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 ...

  7. [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 ...

  8. [LeetCode]题解(python):119 Pascal's Triangle II

    题目来源 https://leetcode.com/problems/pascals-triangle-ii/ Given an index k, return the kth row of the ...

  9. 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, ...

随机推荐

  1. 面试官问:ZooKeeper 有几种节点类型?别再说 4 种啦!

    本文作者:HelloGitHub-老荀 好久没更新 ZK 的文章了,我想死你们啦.之前发布的 HelloZooKeeper 系列文章完结后,项目收获了将近 600 个 star.这远远超过了我自己的预 ...

  2. C语言编程 菜鸟练习100题(11-20)

    [练习11]计算 int, float, double 和 char 字节大小 0. 题目: 计算 int, float, double 和 char 字节大小 1. 分析: 使用 sizeof 操作 ...

  3. java并发编程工具类JUC第四篇:LinkedBlockingQueue链表队列

    在之前的文章中已经为大家介绍了java并发编程的工具:BlockingQueue接口.ArrayBlockingQueue.DelayQueue. LinkedBlockingQueue 队列是Blo ...

  4. mongodb创建用户创建库分配权限

    use unionserver db.createUser({ user: "rshy",pwd: "root1234@......",customData:{ ...

  5. 视频教学动作修饰语:CVPR2020论文解析

    视频教学动作修饰语:CVPR2020论文解析 Action Modifiers: Learning from Adverbs in Instructional Videos 论文链接:https://a ...

  6. Mobileye 自动驾驶策略(一)

    Mobileye 自动驾驶策略(一) 详解 Mobileye 自动驾驶解决方案 Mobileye的自动驾驶解决方案.总得来说,分为四种: Visual perception and sensor fu ...

  7. 智能物联网(AIoT,2020年)(中)

    智能物联网(AIoT,2020年)(中) 05 中国AIoT产业图谱 06 中国AIoT商业模式 标准程度越低人力和时间成本投入越多,2B2C模式附加值高 07 中国AIoT玩家分布简介 四类玩家,优 ...

  8. 激光雷达lidar与点云数据

    激光雷达lidar与点云数据 DEM是分布和显示数字地形的首个广泛使用的机制. 点云是在空间中随机放置的3D点的集合.传感器发出能量脉冲并乘以其返回行程(TWTT,双向行程时间).知道了传感器的位置以 ...

  9. 使用现代C++如何避免bugs(下)

     使用现代C++如何避免bugs(下) About virtual functions Virtual functions hinder a potential problem: the thing ...

  10. SpringCloud-OAuth2(二):实战篇

    如果不了解Oauth2 是什么.工作流程的可以看我上一篇文章: SpringCloud-OAuth2(一):基础篇 这篇讲的内容是:Oauth2在SpringBoot/SpringCloud中的实战. ...