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. php结合jquery异步上传图片(ajaxSubmit)

    php结合jquery异步上传图片(ajaxSubmit),以下为提交页面代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transi ...

  2. 用Unity的Animation播放Animator动画Clip

    简单的动画,其实不需要Animator动画状态机管理,用Animation播放效率更高,但可能由于历史遗留问题,或网上下载的第三方资源,得到的是Animator资源,可以在Clip的Debug试图下, ...

  3. linux系统下的shell脚本

    #!/bin/bash  说明我需要启用bin目录下的bash解释器解释脚本##将第一个文件拷贝到第二个文件,如果出错将错误输出到/dev/null 的空.if判断cp的返回值是否为1,1为成功,0为 ...

  4. log4j配置文件写法

    ### direct log messages to stdout ###log4j.rootLogger=DEBUG,stdoutlog4j.appender.stdout=org.apache.l ...

  5. userInteractionEnabled

    NO -------是自身View下面的按钮之类的能点 YES------是View自身的按钮能点击,他下面的不能点击

  6. <一> MVC - HtmlHelper

    HtmlHelper类位于System.Web.Mvc.Html之中主要有七个静态类组成: FormExtensions - BeginForm, BeginRouteForm, EndForm In ...

  7. C++ 数据类型及相关问题 及输出精度控制

    1.有哪些数据类型? 2.数据类型在不同的编译器会有不同的位宽,如何得知? 使用如下命令: cout<<sizeof(int)<<endl; cout<<sizeo ...

  8. BT5之配置笔记

    BT5本来就是用Ubuntu 10.04做得蓝本,所以,我在配置BT5的时候,基本上都是按照Ubuntu 10.04的配置方法,在配置BT5 1    系统基本设置 1.1  安装Ubuntu10.0 ...

  9. HDU1395+快速幂

    #include<stdio.h> int fast_pow( int a,int b,int mod ){ ; ){ == ){ res = res*a%mod; } a = a*a%m ...

  10. vcastr2.0插件超级简单使用

                Vcastr2.0简单使用 友情提示:1.蓝色文字为必修改内容.2.#字符后面是解释该代码段的主要内容 1. 引用swfobject.js文件 #public/videoplu ...