题目描述:

 

Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle.

Note that the row index starts from 0.

Example:

Input: 3
Output: [1,3,3,1]

Follow up:

Could you optimize your algorithm to use only O(k) extra space?

要完成的函数:

vector<int> getRow(int rowIndex)

说明:

1、这道题给定一个行数rowIndex,要求返回给定行数那一行的帕斯卡三角形,结果存储在vector中。要求空间复杂度不超过O(rowIndex)。

2、做了前面的帕斯卡三角形的生成那道题目,实现这道题目也就易如反掌了。

同样的方法,我们逐行生成每一行的帕斯卡三角形,到达指定行数时,停下来,返回vector就可以了。

代码如下:

    vector<int> getRow(int rowIndex)
{
vector<int>res={1};
int i;
while(rowIndex--)//如果rowIndex==0,那么不会进入循环,如果rowIndex==1,进入循环一次。
{//while(rowIndex--)这种写法,是先判断rowIndex是不是0,如果不是那么进入循环,最后再减一;如果是,那么不进入循环。
i=res.size()-1;
while(i>0)
{
res[i]=res[i]+res[i-1];
i--;
}
res.push_back(1);
}
return res;
}

上述代码实测3ms,beats 80.79% of cpp submissions。

leetcode-119-Pascal's Triangle II(生成某一行的帕斯卡三角形)的更多相关文章

  1. [LeetCode] 119. Pascal's Triangle II 杨辉三角 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 杨辉三角之二

    Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note t ...

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

  4. C#解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  ...

  5. LeetCode 119 Pascal's Triangle II

    Problem: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Ret ...

  6. leetcode 119 Pascal's Triangle II ----- java

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

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

  8. Java for 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 ...

  9. Leetcode 119 Pascal's Triangle II 数论递推

    杨辉三角,这次要输出第rowIndex行 用滚动数组t进行递推 t[(i+1)%2][j] = t[i%2][j] + t[i%2][j - 1]; class Solution { public: ...

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

随机推荐

  1. CecOS Virtualization

    CecOS CecOS 是 OPENFANS 为中小型企业提供企业开源云计算解决方案的基础框架:CecOSvt 是一款运行在社区企业云操作系统(CecOS)上的虚拟化开放式解决方案. 也可用演示模式. ...

  2. Idea安装lombok插件及使用

    安装lombok插件:File-settings 具体步骤如下图:1 2.找到Plugins 然后在搜索栏里搜索lombok 点击下放的Search in repositories 3.选中lombo ...

  3. 一张图记住TCP/IP通讯中的IP地址配置

    TCP/IP通讯情景: 用网线将计算机A(服务器Server)和计算机B(Client)连接起来.程序代码在计算机A中,计算机B中安装有TCP/IP通讯助手. (图中屏幕大的是计算机A,屏幕小的笔记本 ...

  4. Linux 系统配置

    centos 7 防火墙和端口配置开启redis端口,修改防火墙配置文件 vi /etc/sysconfig/iptables 加入端口配置 -A RH-Firewall-1-INPUT -m sta ...

  5. HTTP/2协议

    一.简介 http://toutiao.com/a6258917202906104066/

  6. [C#] IEnumerable vs IQueryable

    这篇博客将介绍IEnumerable和IQueryable之间的区别. 1. IQueryable是继承自IEnumerable接口的.所以IEnumerable能做的,IQueryable都能做. ...

  7. (1)WePHP 开启WePHP

    新建入口文件index.php,定义新项目的目录地址APP_PATH,引入WePHP项目入口文件 <?php define('APP_PATH','./index/'); require_onc ...

  8. python可视化

    1.折线图 import matplotlib.pyplot as plt x = [1,2,3,4,5] squares = [1,4,9,16,25] plt.plot(x,squares) pl ...

  9. 编写高质量代码改善C#程序的157个建议——建议86:Parallel中的异常处理

    建议86:Parallel中的异常处理 建议85阐述了如何处理Task中的异常.由于Task的Start方法是异步启动的,所以我们需要额外的技术来完成异常处理.Parallel相对来说就要简单很多,因 ...

  10. gitlab发送邮件配置

    在使用gitlab过程中,通常需配置邮件来实现代码管理服务器向成员(member)发送邮件.本文将实现gitlab中邮件配置 1.编辑 /etc/gitlab/gitlab.rb,修改配置邮件,这里以 ...