题目描述:

 

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:

  1. Input: 3
  2. 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就可以了。

代码如下:

  1. vector<int> getRow(int rowIndex)
  2. {
  3. vector<int>res={1};
  4. int i;
  5. while(rowIndex--)//如果rowIndex==0,那么不会进入循环,如果rowIndex==1,进入循环一次。
  6. {//while(rowIndex--)这种写法,是先判断rowIndex是不是0,如果不是那么进入循环,最后再减一;如果是,那么不进入循环。
  7. i=res.size()-1;
  8. while(i>0)
  9. {
  10. res[i]=res[i]+res[i-1];
  11. i--;
  12. }
  13. res.push_back(1);
  14. }
  15. return res;
  16. }

上述代码实测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. 494. Target Sum 添加标点符号求和

    [抄题]: You are given a list of non-negative integers, a1, a2, ..., an, and a target, S. Now you have ...

  2. 使用lucene query的CharFilter 去掉字符中的script脚本和html标签

    1.准备数据,这里我从数据库读取一个带有html标签和script脚本的数据

  3. 两个线程并发执行以下代码,假设a是全局变量,那么以下输出______是不可能的?

    3.两个线程并发执行以下代码,假设a是全局变量,那么以下输出______是不可能的? void foo(){    ++a;    printf("%d ",a);}A.3 2   ...

  4. Windows下搭建JSP开发环境

    1. 配置说明: => 编辑器: Eclipse (Java EE IDE) => 数据库: MySQL (MySQL Workbench 进行数据库管理, 用 MySQL Connect ...

  5. mysql:unknown variable 'default -collation=utf8_general_ci'

    无法登陆,因为在配置文档中设置了默认编码方式 将它注释掉,问题就解决了 在utf8_bin中你就找不到 txt = 'A' 的那一行, 而 utf8_general_ci 则可以.utf8_gener ...

  6. VMware vSphere之vCenter

    1.vCenter是什么? vCenter Server 是 vSphere管理员使用的主要管理工具,首先,通过它可实现对数据中心所有主机的单点控制,也就是提供集中式管理.其次,提供基本的基础结构操作 ...

  7. 云存储上传控件(cloud2)-Xproer.HttpUploader7

    版权所有 2009-2016 荆门泽优软件有限公司 保留所有权利 官方网站:http://www.ncmem.com/ 产品首页:http://www.ncmem.com/webapp/cloud2/ ...

  8. http 介绍

    https://www.cnblogs.com/ranyonsue/p/5984001.html 关于HTTP协议,一篇就够了 HTTP简介 HTTP协议是Hyper Text Transfer Pr ...

  9. linux 命令学习-网络相关配置

    网络配置相关 网卡配置文件:etc/sysconfig/network-scripts/ifcfg-eth0 DNS 配置文件 etc/resolv.conf 主机配置文件 etc/sysconfig ...

  10. 洛谷P4234 最小差值生成树(lct动态维护最小生成树)

    题目描述 给定一个标号为从 11 到 nn 的.有 mm 条边的无向图,求边权最大值与最小值的差值最小的生成树. 输入输出格式 输入格式:   第一行两个数 n, mn,m ,表示图的点和边的数量. ...