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. Reactor模式(反应器模式)

    Reactor这个词译成汉语还真没有什么合适的,很多地方叫反应器模式,但更多好像就直接叫reactor模式了,其实我觉着叫应答者模式更好理解一些.通过了解,这个模式更像一个侍卫,一直在等待你的召唤,或 ...

  2. 2D UI和3D UI的工作原理

    2D UI的工作原理 UI控件的位置在UI Root 的红框(视窗)上,也就是UI控件的z轴,相机的z轴,UI Root的z轴都是0,因为2D UI都是纯粹的2D图片按层次显示,不会不出现三维立体效果 ...

  3. C# - implicit, explicit

    如果类型直接没有继承关系,也没有共享接口,想在这两个类型之间进行转换,就必须重载转换运算符. 此时需要关键字implicit和explicit. 下面定义了类型ConvClass1和ConvClass ...

  4. unity 基础之PhysicsManager

    原地址:http://www.cnblogs.com/alongu3d/p/3644725.html @by  广州小龙 1.Gravity(重力) 物理系统都是根据实际情况进行模拟的,由于Y值是往下 ...

  5. 介绍一个超好用的HICHARTS扩展插件

    因为需要,所以HIGHCHARTS了解一下是很有必要的. 但原始应用确实效率不行. 刚好,现在有个需求是从一系列的JSON里抽出表格数据,再显示图形. jquery.highchartsTable.j ...

  6. UVA 10896 Sending Email

    这个题目真是伤透脑筋了,一直RE,连着改了好几个版本,又是spfa,又是单调队列dijkstra+单调队列,总是不过,后来发现M开小了,双向边应该开m的两倍,悲剧啊!!!以后不管怎样,数组一定要尽量开 ...

  7. asp中将文本框内的日期转换成datetime类型的数据

    将字符类型的日期转化为DateTime类型主要有以下方法: 方法一:Convert.ToDateTime(string) string格式有要求,必须是yyyy-MM-dd hh:mm:ss 方法二: ...

  8. Qt中QString,int,char,QByteArray之间相互转换

    http://blog.csdn.net/ymc0329/article/details/7284514 int  转 QString int m=1; QString b; b=QString::n ...

  9. Android 如何在ScrollView中嵌套ListView

    前几天因为项目的需要,要在一个ListView中放入另一个ListView,也即在一个ListView的每个ListItem中放入另外一个ListView.但刚开始的时候,会发现放入的小ListVie ...

  10. Android-UGallery

    public class UGallery extends Gallery { public UGallery(Context context, AttributeSet attrs) { super ...