Given an index k, return the kth row of the Pascal's triangle.

For example, given k = 3,
Return [1,3,3,1].

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

思路:从右往左遍历并填写三角形的图结构

class Solution {
public:vector<int> getRow(int rowIndex) {vector<int> a(rowIndex + 1);a[0] = 1;for(int i = 1; i <= rowIndex; i++) //遍历行for(int j = i; j >= 0; j--) //遍历行中的元素,第k行有k个元素。注意行中元素需要从右往左遍历,否则下一行填写的结果会覆盖上一行还未使用的元素if (j == i)a[j] = a[j-1];else if (j == 0)a[j] = a[j];elsea[j] = a[j-1] + a[j];return a;                    }
};

转载于:https://www.cnblogs.com/qionglouyuyu/p/4854608.html

119. Pascal's Triangle II (Graph; WFS)相关推荐

  1. [LeetCode]119.Pascal's Triangle II

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/SunnyYoona/article/details/43562603 题目 Given an ind ...

  2. LeetCode 119. Pascal’s Triangle II

    119. Pascal's Triangle II My Submissions QuestionEditorial Solution Total Accepted: 72147 Total Subm ...

  3. Leet Code OJ 119. Pascal's Triangle II [Difficulty: Easy]

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

  4. LeetCode - Easy - 119. Pascal‘s Triangle II

    Topic Array Description https://leetcode.com/problems/pascals-triangle-ii/ Given an integer rowIndex ...

  5. leetcode python3 简单题119. Pascal's Triangle II

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百一十九题 (1)题目 英文: Given a non-negative ind ...

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

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

  8. [Array]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. 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. 如何在CPU上优化GEMM矩阵乘法
  2. python基础 继承
  3. python的编译器有哪些-python的编译器有哪些
  4. 对信号集操作函数的使用方法和顺序
  5. flink 1-个人理解
  6. win7,ubuntu双系统——重装win7后如何恢复ubuntu引导
  7. 文件上传函数(get post assert)
  8. JS和Jquery获取和修改label的值
  9. java 多个timer_java – Timer正在创建多个计时器实例
  10. getnumdevices.c setgetdevicetype例程
  11. ubuntu14.04 搭建 tftp 服务器
  12. 如何在CAD中利用块实现坐标、高程的自动标注
  13. window.opener 与 window.dialogArguments的用法
  14. cad画流程图的插件_PIDCAD工艺流程图绘图软件
  15. GIT提交代码到远程创库
  16. python程序设计的选择题_智慧树Python程序设计基础选择题答案
  17. 美赞臣中国独立后任命朱定平为总裁;思拓凡亚洲首个技术研发卓越中心上海揭幕 | 美通社头条...
  18. 腾讯、字节、蘑菇街、阿里实习生面经(面试记录)(2)
  19. aria2 txt导入_aria2 让下载提速
  20. 电蚊拍 电路图 原理

热门文章

  1. Akka Cluster原理与应用
  2. C#调用dll提示试图加载格式不正确的程序解决方法
  3. C#.Net网页加载等待效果漂亮并且简单
  4. python全栈学习--day40()
  5. 服务器出现 nginx 502 Bad Gateway
  6. 河南省第七届省赛 问题 F: Turing equation 水题
  7. 几句话描述简单算法——排序与搜索
  8. Unity学习笔记 - Assets, Objects and Serialization
  9. 15.用PHP写出显示客户端IP与服务器IP的代码,如何防止用户使用代理的情况?[添加更多详情]...
  10. C# 文件保存到数据库中或者从数据库中读取文件