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. 10.13 nc:多功能网络工具

    nc命令 是一个简单.可靠.强大的网络工具,它可以建立TCP连接,发送UDP数据包,监听任意的TCP和UDP端口,进行端口扫描,处理IPv4和IPv6数据包.     如果系统没有nc命令,那么可以手 ...

  2. mysql中的实例、数据库关系简介

    MySQL是单进程多线程(而Oracle等是多进程),也就是说MySQL实例在系 统上表现就是一个服务进程,即进程(通过多种方法可以创建多实例,再安装一个端口号不同的mysql,或者通过workben ...

  3. DLPack构建跨框架的深度学习编译器

    DLPack构建跨框架的深度学习编译器 Tensorflow,PyTorch和ApacheMxNet等深度学习框架提供了一个功能强大的工具包,可用于快速进行原型设计和部署深度学习模型.易用性通常是以碎 ...

  4. 摄像头定位:ICCV2019论文解析

    摄像头定位:ICCV2019论文解析 SANet: Scene Agnostic Network for Camera Localization 论文链接: http://openaccess.the ...

  5. 激光雷达Lidar Architecture and Lidar Design(上)

    激光雷达Lidar Architecture and Lidar Design(上) 介绍 激光雷达结构: 基本条件 构型和基本布置 激光雷达设计: 基本思想和基本原则 总结 介绍 激光雷达结构是激光 ...

  6. Excel创建手机号1000个

    一.输入手机号15900000001,并且选择 二.选中这一列 三.按住Ctrl,鼠标选中创建手机号,取消选中的状态 四.开始-填充 五.选择序列 六.选择列-自动填充-点击确定 七.查看结果,生成成 ...

  7. hashmap专题

    hashmap重要变量 源码中定义了很多常量,有几个是特别重要的. DEFAULT_INITIAL_CAPACITY: Table数组的初始化长度: 1 << 4,即 2^4=16(这里可 ...

  8. MySQL:数据库优化,看这篇就够了

    数据库优化一方面是找出系统的瓶颈,提高MySQL数据库的整体性能,而另一方面需要合理的结构设计和参数调整,以提高用户的相应速度,同时还要尽可能的节约系统资源,以便让系统提供更大的负荷. 1. 优化一览 ...

  9. NOIP模拟测试39,思维禁锢专场「工业题·玄学题·卡常题」

    工业题 题解 抱歉,题解没时间写了 代码 #include<bits/stdc++.h> using namespace std; #define ll long long #define ...

  10. 手写Spring Config,最终一战,来瞅瞅撒!

    上一篇说到了手写Spring AOP,来进行功能的增强,下面本篇内容主要是手写Spring Config.通过配置的方式来使用Spring 前面内容链接: 我自横刀向天笑,手写Spring IOC容器 ...