题目来自于Leetcode

https://leetcode.com/problems/pascals-triangle/

Given numRows, generate the first numRows of Pascal's triangle.

For example, given numRows = 5,

Return

[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int> >res;
for(int i=0;i<numRows;i++)
{
vector<int>vec(i+1,1);
if(i>1)
for(int j=1;j<i;j++)
vec[j]=res[i-1][j-1]+res[i-1][j];
res.push_back(vec);
vector<int>().swap(vec);
}
return res;
}
};

Pascal's Triangle II

Total Accepted: 42320 Total
Submissions: 143760

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?

此处有内存要求尽管採用第一种方法能够ac可是明显不符合要求

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

我们必须又一次设计算法。

第一想到的就是pascal三角形的系数会等于N行i列的值等于
( r
     n )


可是
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int>res(rowIndex+1,1);
if(rowIndex<2)
return res;
long long nth=1;
for(int i=1;i<rowIndex+1;i++)
nth*=i;
long long rth=1,n_rth=nth;
for(int i=1;i<rowIndex;i++)
{ n_rth/=(rowIndex-i+1);
res[i]=nth/rth/n_rth;
rth*=(i+1);
}
return res;
}
};

用来存储二项式系数的值非常easy在rowIndex=24时候就报错了

最后一种也就是正确的方法是利用分配的空间来计算的详细给出了k=5的详细描写叙述

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhvdXllbGlodWE=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">



class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int>res(rowIndex+1,1);
if(rowIndex<2)
return res;
int t1,t2;
for(int i=2;i<=rowIndex;i++)
{
t1=res[0];
t2=res[1];
for(int j=1;j<i+1;j++)
{
res[j]=t1+t2;
t1=t2;
t2=res[j+1];
}
res[i]=1;
}
return res;
}
};



My Submissions

Question
 Solution 

Pascal&#39;s Triangle I,II的更多相关文章

  1. 【Leetcode】Pascal&#39;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——Pascal&#39;s Triangle II

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

  3. Pascal&#39;s Triangle II

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

  4. [LeetCode]Pascal&#39;s Triangle II

    题目:给定一个行索引index,返回帕斯卡三角形第index层的三角形 算法:生成index层帕斯卡三角形,并返回第index层三角形 public class Solution { public L ...

  5. LeetCode——Pascal&#39;s Triangle

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

  6. leetcode笔记:Pascal&#39;s Triangle

    一. 题目描写叙述 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...

  7. leetcode - Pascal&#39;s Triangle

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

  8. LeetCode118:Pascal&#39;s Triangle

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

  9. 【leetcode】118. Pascal&#39;s Triangle

    @requires_authorization @author johnsondu @create_time 2015.7.23 19:54 @url [Pascal's Triangle](http ...

随机推荐

  1. 【字符串哈希】The 16th UESTC Programming Contest Preliminary F - Zero One Problem

    题意:给你一个零一矩阵,q次询问,每次给你两个长宽相同的子矩阵,问你它们是恰好有一位不同,还是完全相同,还是有多于一位不同. 对每行分别哈希,先一行一行地尝试匹配,如果恰好发现有一行无法对应,再对那一 ...

  2. nginx_lua vs nginx+php 应用场景

    在我的印象中很多人还是选择nginx+php这种组合搭配,你的选择是nginx+lua,那么nginx+lua比和php的组合优势在哪里?清无:首先,Nginx+php之间是要有进程之间通信的,这样以 ...

  3. word-ladder总结

    title: word ladder总结 categories: LeetCode tags: 算法 LeetCode comments: true date: 2016-10-16 09:42:30 ...

  4. python开发_HTMLParser_html文档解析

    ''' 在HTMLParser类中,定义了很多的方法,但是很多方法都是没有实现的, 这需要我们继承HTMLParser类,自己去实现一些方法 如: # Overridable -- handle st ...

  5. Go语言Web框架gwk介绍 (三)

    上一篇忘了ChanResult ChanResult 可以用来模拟BigPipe,定义如下 type ChanResult struct { Wait sync.WaitGroup Chan chan ...

  6. 电感式升压转换器-AIC1896 电感式升压转换器

    电感式升压转换器-AIC1896 AIC1896是一个脉冲宽度调变(Pulse-Width-Modulation;PWM)控制之升压型转换器,它可以提供一个定电流以驱动白光LED. (图五A)为升压转 ...

  7. jquery实现回车键触发事件

    键盘事件有3: keydown,keypress,keyup,分别是按下,按着没上抬,上抬键盘 . 正确代码为: $(document).keyup(function(event){ if(event ...

  8. hdu 4647 Another Graph Game,想到了就是水题了。。

    题目是给一个无向图,其中每个节点都有点权,边也有边权,然后就有2个小朋友开始做游戏了ALICE &BOB 游戏规定ALICE 先行动然后是BOB,然后依次轮流行动,行动时可以任意选取一个节点并 ...

  9. 如何使用==操作符,Equals方法,ReferenceEquals方法,IEquatable接口比较2个对象

    "世界上不会有两片完全相同的树叶",这句话适用于现实世界.而在软件世界中,这句话变成了"世界上必须有两片完全相同的树叶",否则,很多事情无以为继. 当比较2个对 ...

  10. MVC使用Dotnet.HighCharts做图表01,区域图表

    如果想在MVC中使用图表显示的话,DotNet.HighCharts是不错的选择.DotNet.HighCharts是一个开源的JavaScript图表库,支持线型图表.柱状图标.饼状图标等几十种图标 ...