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?

注意:只开辟O(K)的内存空间,所以只保存前一组数列和当前数组,并不断滚动更新即可。

class Solution {
public:
vector<int> getRow(int rowIndex) {
// Note: The Solution object is instantiated only once and is reused by each test case.
vector<int> res;
vector<int> pre;
if(rowIndex == 0)
{
res.push_back(1);
return res;
}
pre.push_back(1);
for(int i = 1; i <= rowIndex; i++)
{
res.push_back(1);
for(int j = 1; j < i+1; j++)
{
res.push_back( pre[j-1] + (j < pre.size() ? pre[j] : 0) );
}
pre.clear();
for(int k = 0; k < res.size(); k++)
{
pre.push_back(res[k]);
}
res.clear();
}
return pre;
}
};

【LeetCode】Pascal's Triangle II (杨辉三角)的更多相关文章

  1. [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, ...

  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 杨辉三角 II

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

  4. Leetcode#118. Pascal's Triangle(杨辉三角)

    题目描述 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], [1,2, ...

  5. LeetCode 118. Pascal's Triangle (杨辉三角)

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

  6. 每天一道LeetCode--118. Pascal's Triangle(杨辉三角)

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

  7. LeetCode Pascal's Triangle II (杨辉三角)

    题意:给出杨辉三角的层数k,返回最后一层.k=0时就是只有一个数字1. 思路:滚动数组计算前一半出来,返回时再复制另一半.简单但是每一句都挺长的. 0ms的版本: class Solution { p ...

  8. LeetCode(119):杨辉三角 II

    Easy! 题目描述: 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 3 输出: [1,3,3,1] 进阶: ...

  9. LeetCode: Pascal's Triangle II 解题报告

    Pascal's Triangle II Total Accepted: 19384 Total Submissions: 63446 My Submissions Question Solution ...

  10. 学会从后往前遍历,例 [LeetCode] Pascal's Triangle II,剑指Offer 题4

    当我们需要改变数组的值时,如果从前往后遍历,有时会带来很多麻烦,比如需要插入值,导致数组平移,或者新的值覆盖了旧有的值,但旧有的值依然需要被使用.这种情况下,有时仅仅改变一下数组的遍历方向,就会避免这 ...

随机推荐

  1. 【Android】使用Pull生成/解析XML文件

    一.生成XML文件,即是将对象集合转为XML文件存储. 对象集合 –> XML(序列化) Android中使用android.util.Xml类对其进行了描述,提供相应的API. 步骤大致如下: ...

  2. Liunx 安装 Mysql 5.7

    #[安装 Mysql 5.7] # 00.系统目录说明# 安装文件下载目录:/data/software# Mysql目录安装位置:/usr/local/mysql# 数据库保存位置:/data/my ...

  3. java——常用类的总结

    package test; import java.util.ArrayList; import java.util.HashMap; import java.util.Set; public cla ...

  4. uboot中CMD的实现

    CMD配置位于config_cmd_default.h   configs/at91/sam9g10ek.h 头文件位于include/command.h 41 struct cmd_tbl_s {  ...

  5. java- 控制double输出的小数点位数

    像C语言直接  printf("%f.02",float); 非常简单,还可以控制输出的缩距,很是方便. Java就不一样了,但是java也有它的方便之处 下面用列子来解释,用到的 ...

  6. 05 Oracle process

    本章提要----------------------------------------------所有的 process 都是在 PGA 内(memory)server process: 与 cli ...

  7. 指定Android Studio编译工程时的源文件编码

    统一设置为UTF8编码在工程的根目录下的build.gradle文件中,添加定义.tasks. withType(JavaCompile) {    options.encoding = " ...

  8. ioctl参数cmd=2错误

    在写内核驱动的时候,用到了ioctl.自己定义cmd作为ioctl的参数.如下: enum CMD { LEVEL_DOWN, LEVEL_UP, GPIO_INPUT, GPIO_OUTPUT, G ...

  9. C++ Primer学习笔记(一)

    始终对C++念念不忘,看过 一个32岁入门的70后程序员给我的启示  之后,心情激荡,更是一发不可收拾. 认真地说,我不是一个执着的人,见异思迁,好读书而不求甚解,兼之情绪化(~~ 某些方面),于是怒 ...

  10. e1087. 用For循环做数组的遍历

    The for statement can be used to conveninently iterate over the elements of an array. The general sy ...