1.题目描述

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?

2.解法分析

题目说要优化空间需求,实际上就是要复用空间,于是写出的代码如下:

class Solution {

public:

    vector<int> getRow(int rowIndex) {

        // Start typing your C/C++ solution below

        // DO NOT write int main() function\

        //areslipan

        vector<int>curRow;

        curRow.push_back(1);

        if(rowIndex == 0)return curRow;

        curRow.push_back(1);

        if(rowIndex == 1)return curRow;

        

        vector<int>result;

        result.assign(rowIndex+1,1);

        

        int cur;

        int nextCur;

        for(int i = 2;i<=rowIndex;++i)

        {

            cur = result[0];

            for(int j =1;j<i;++j)

            {

                nextCur = result[j];

                result[j]=cur+result[j];

                cur = nextCur;

            }

        }

        

        return result;

        

        

    }

};

Pascal's Triangle II的更多相关文章

  1. 28. Triangle && Pascal's Triangle && Pascal's Triangle II

    Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to a ...

  2. 【LeetCode】118 & 119 - Pascal's Triangle & Pascal's Triangle II

    118 - Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, ...

  3. Pascal's Triangle,Pascal's Triangle II

    一.Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, giv ...

  4. 杨辉三角形II(Pascal's Triangle II)

    杨辉三角形II(Pascal's Triangle II) 问题 给出一个索引k,返回杨辉三角形的第k行. 例如,给出k = 3,返回[1, 3, 3, 1] 注意: 你可以优化你的算法使之只使用O( ...

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

  6. 118/119. Pascal's Triangle/II

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

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

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

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

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

  9. LeetCode Pascal's Triangle && Pascal's Triangle II Python

    Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given ...

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

随机推荐

  1. 002.TPerlRegEx简单测试

    我要做什么? 将一个字符串中的所有连续的数字替换成一个* 代码: program Project1; {$APPTYPE CONSOLE} uses System.SysUtils, PerlRegE ...

  2. Linux统计某文件夹下文件、文件夹的个数

    统计某文件夹下文件的个数 ls -l |grep "^-"|wc -l 统计某文件夹下目录的个数 ls -l |grep "^d"|wc -l 统计文件夹下文件 ...

  3. STL set_difference set_intersection set_union 操作

    以下是STL algorithm的几个函数,使用的条件是有序容器,所以 vector在被sort了之后是可以使用的,set也是可以使用的. set_difference 这个是求得在第一个容器中有,第 ...

  4. UML用户指南--UML图简介

    本节和大家一起学习一下UML图,这里主要介绍UML结构图和UML行为图两部分,下面让我们一起看一下UML图的详细介绍吧. UML图 这里再次提到对软件体系结构进行可视化.详述.构造和文档化,有5种最重 ...

  5. MongoDB索引介绍

    MongoDB中的索引其实类似于关系型数据库,都是为了提高查询和排序的效率的,并且实现原理也基本一致.由于集合中的键(字段)可以是普通数据类型,也可以是子文档.MongoDB可以在各种类型的键上创建索 ...

  6. 纯CSS3代码实现表格奇偶行异色,鼠标悬浮变色

    1.首先会用到<tr></tr>元素两个伪类,nth-child()和hover. 然后需要注意的是伪类都是通过冒号引用的,不是点号,即tr:hover{} 其次,CSS代码中 ...

  7. sjtu1285 时晴时雨

    Description Taring 喜欢晴天,也喜欢雨天. Taring说:我想体验连续的\(K\)天的晴朗,去远足,去放歌:我还想再这\(K\)个晴天之后,再去体验连续的K天的云雨,去感受落雨时的 ...

  8. HDU4524+水题

    简单. #include<stdio.h> #include<string.h> ; int a[ maxn ]; int main(){ int ca; scanf(&quo ...

  9. 一些有用的webservice

    http://developer.51cto.com/art/200908/147125.htm 下面总结了一些常用的Web Service,是平时乱逛时收集的,希望对大家有用. ========== ...

  10. Android Service 生命周期和使用注意项

    一.基础知识 服务一般分为两种: 1:本地服务, Local Service 用于应用程序内部.在Service可以调用Context.startService()启动,调用Context.stopS ...