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. Build Tree View Structure for SharePoint List Data

    博客地址 http://blog.csdn.net/foxdave 此文参考自->原文链接 版权归原作者所有,我只是进行一下大致的翻译 应坛友要求,帮助验证一下功能. SharePoint列表数 ...

  2. PostgreSQL脱敏示例

    mydb=# create table test_desensitization(id integer, name varchar(32), phone_num varchar(11)); CREAT ...

  3. c++的c风格字符串函数的实现

    要注意使用断言判断传入的字符串非空. #include <cassert> //求字符串长度 size_t StrLen(const char *str) { assert(str != ...

  4. .Net脱壳工具 de4dot参数说明/简易教程

    de4dot  /? 帮助原文 使用方法 de4dot "d:\xx.exe" -p xc -p xc 指定壳类型 , 这里是xc,表示Xenocode壳.这样会在exe的相同目录 ...

  5. Swift 获取plist文件展示在TableView上

    // 1.定义二维数组 var data:[[String]]! override func viewDidLoad() { super.viewDidLoad() // 2.实例化tableView ...

  6. Codeforces 1006C:Three Parts of the Array(前缀和+map)

    题目链接:http://codeforces.com/problemset/problem/1006/C (CSDN又改版了,复制粘贴来过来的题目没有排版了,好难看,以后就截图+题目链接了) 题目截图 ...

  7. .NET 中让 Task 支持带超时的异步等待

    Task 自带有很多等待任务完成的方法,有的是实例方法,有的是静态方法.有的阻塞,有的不阻塞.不过带超时的方法只有一个,但它是阻塞的. 本文将介绍一个非阻塞的带超时的等待方法.   Task 已有的等 ...

  8. Linux 脚本编写

    第一个shell脚本编写 #!/bin/bash # 上面中的 #! 是一种约定标记, 它可以告诉系统这个脚本需要什么样的解释器来执行; #定义变量: APP_BASE_PATH="/opt ...

  9. web本地存储(localStorage、sessionStorage)

    web 本地存储 (localStorage.sessionStorage) 说明 对浏览器来说,使用 Web Storage 存储键值对比存储 Cookie 方式更直观,而且容量更大,它包含两种:l ...

  10. mysql索引优化续

    (1)索引类型: Btree索引:抽象的可以理解为“排好序的”快速查找结构myisam,innodb中默认使用Btree索引 hash索引:hash索引计算速度非常的快,但数据是随机放置的,无法对范围 ...