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?

思路:最简单的方法就是依照【Leetcode】Pascal's
Triangle
 的方式自顶向下依次求解,但会造成空间的浪费。若仅仅用一个vector存储结果。在下标从小到大的遍历过程中会改变vector的值,如[1, 2, 1] 按小标从小到大累计会变成[1, 3, 4, 1]不是所求解,因此能够採取两种解决方法,简单的一种则是使用两个vector,一个存上一层的值。一个存本层的值,然后再进行交换。第二种方法则是按下标从大到小进行遍历,则会避免上述问题。

class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> result; for(int i = 0; i <= rowIndex; i++)
{
for(int j = i - 1; j > 0; j--)
result[j] = result[j] + result[j - 1]; result.push_back(1);
} return result;
}
};

【Leetcode】Pascal&#39;s Triangle II的更多相关文章

  1. 【LeetCode】Pascal's Triangle II 解题报告

    [LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...

  2. 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)

    [LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...

  3. 【leetcode】998. Maximum Binary Tree II

    题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than ...

  4. 【leetcode】963. Minimum Area Rectangle II

    题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...

  5. 【LeetCode】142. Linked List Cycle II

    Difficulty:medium  More:[目录]LeetCode Java实现 Description Given a linked list, return the node where t ...

  6. 【LeetCode】119. 杨辉三角 II Pascal‘s Triangle II(Python & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 方法一: 空间复杂度 O ( k ∗ ( k + 1 ...

  7. 【leetcode】Pascal's Triangle II

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

  8. 【leetcode】Pascal's Triangle I & II (middle)

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

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

随机推荐

  1. openssl之BIO系列之5---CallBack函数及其控制

    CallBack函数及其控制     ---依据openssl doc/crypto/bio/bio_set_callback.pod翻译和自己的理解写成          (作者:DragonKin ...

  2. C++ STL copy函数效率分析

    在C++编程中,经常会配到数据的拷贝,如数组之间元素的拷贝,一般的人可能都会用for循环逐个元素进行拷贝,在数据量不大的情况下还可以,如果数据量比较大,那么效率会比较地下.而STL中就提供了一个专门用 ...

  3. 结合使用AngularJS和Django

    原地址 好吧,我承认自己很懒,时间又不够用. 翻译的几个文章都是虎头蛇尾,但我保证这次肯定不太监. 关键的单词不翻译,实在觉得翻译成汉语很别扭,括号里是参考翻译. 有问题和建议尽管提出来,我会改进完善 ...

  4. Linux查看硬盘使用时间等信息

    查看硬盘信息的很多命令,都需要root权限,如果普通用户无法看到信息,请切换至root: 1.查看硬盘使用时间等信息 硬盘使用时间很重要,硬盘理论寿命是3万小时以上 $ sudo smartctl - ...

  5. WinCE隐藏显示任务栏,当任务栏隐藏时将其显示,当任务栏显示时将其隐藏(FindWindow,ShowWindow,IsWindowVisible),

    HANDLE hWndTaskBar = ::FindWindow(TEXT("HHTaskBar"), NULL);  if(::IsWindowVisible(hWndTask ...

  6. vim在编译器 . 命令(点命令)

    时间:2014.06.28 地点:基地 -------------------------------------------------------------------------------- ...

  7. Codeforces Round #277.5 (Div. 2)B——BerSU Ball

    B. BerSU Ball time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  8. HGE基础教程

    作者:寰子 来源:http://www.hgechina.com/前言: 写道: 无意中发现了hge中文社区,听朋友介绍,认识了hge,然后开始对它进行研究,并使用hge开始制作游戏. 因为我所得的资 ...

  9. A First Exploration Of SolrCloud

    A First Exploration Of SolrCloud Update: this article was published in August 2012, before the very ...

  10. 深入了解HTTP协议、HTTP协议原则

    ttp协议学习系列 1. 基础概念篇 1.1 介绍 HTTP是Hyper Text Transfer Protocol(超文本传输协议)的缩写. 它的发展是万维网协会(World Wide Web C ...