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 + ); a[] = ;
for(int i = ; i <= rowIndex; i++) //遍历行
for(int j = i; j >= ; j--) //遍历行中的元素,第k行有k个元素。注意行中元素需要从右往左遍历,否则下一行填写的结果会覆盖上一行还未使用的元素
if (j == i)
a[j] = a[j-];
else if (j == )
a[j] = a[j];
else
a[j] = a[j-] + a[j]; return a;
}
};

119. Pascal's Triangle II (Graph; WFS)的更多相关文章

  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. 118/119. Pascal's Triangle/II

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

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

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

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

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

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

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

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

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

随机推荐

  1. 右键添加git bush here

    由于sourcetree自动安装的git,导致右键没有git bush here,那么我们就自己添加一下. 运行regedit.exe进入注册表,在HKEY_CLASSES_ROOT\Director ...

  2. BZOJ3436: 小K的农场(差分约束裸题&DFS优化判环)

    3436: 小K的农场 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2111  Solved: 986[Submit][Status][Discus ...

  3. WIN 10环境下JDK的安装和环境配置

    在做测试的过程中,诸如Selenium.Appium.Macaca.Airtest.RobotFramework.Jmeter等框架或工具都需要用到一样基础的环境JAVA JDK.最近刚好换了电脑,就 ...

  4. python中如何将两个list合并成一个list,不用for语句

    1, add 2, 用list的extend方法,L1.extend(L2),该方法将参数L2的全部元素添加到L1的尾部,例如: 3, 用切片(slice)操作,L1[len(L1):len(L1)] ...

  5. 排列组合算法(基于c++实现)

    排列 全排列是将一组数按一定顺序进行排列,如果这组数有n个,那么全排列数为n!个.现以{1, 2, 3}为例说明如何编写全排列的递归算法 第一层S1表示第一个数分别与第1.2.3个数交换位置,如123 ...

  6. Java多线程编程核心技术,第四章

    1,ReentrantLock 2,object的wait(),wait(x),notify(),notifyAll(),分别等于Condition的await(),await(x,y),signal ...

  7. docker挂载本地目录和数据卷容器

    1.docker挂载本地目录 docker可以支持把一个宿主机上的目录挂载到镜像里. 交互模式运行docker run -it -v /home/dock/Downloads:/usr/Downloa ...

  8. 现在就开始使用AngularJS的三个重要原因

    现在就开始使用AngularJS的三个重要原因 在线演示1 本地下载 如果你不熟悉什么是Angular.js的话,小编我强烈推荐你阅读 Javascript教程:AngularJS的五个超酷特性.简单 ...

  9. 简单桶排序算法-python实现

    #-*- coding: UTF-8 -*- import numpy as np def BucketSort(a, n): barrel = np.zeros((1, n), dtype = 'i ...

  10. vim配置之tagbar

    vimConfig/plugin/tagbar-setting.vim let g:tagbar_width=4 map <F12> :TagbarToggle<CR> map ...