题目来自于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. 「HNOI2018」毒瘤

    「HNOI2018」毒瘤 解题思路 先考虑只有一棵树的情况,经典独立集计数. \[ dp[u][0]=\prod (dp[v][0]+dp[v][1]) \\ dp[u][1]=\prod dp[v] ...

  2. BZOJ 3130: [Sdoi2013]费用流 网络流 二分 最大流

    https://www.lydsy.com/JudgeOnline/problem.php?id=3130 本来找费用流的题,权当复习一下网络流好了. 有点麻烦的是double,干脆判断大小或者二分增 ...

  3. 【扩展欧几里得】BZOJ1477-青蛙的约会

    一直在WA,后来我发现我把东西看反了…… [题目大意] 给出一个长度为L的环状坐标轴,两个点开始时位于(X,0).(Y,0).每次两点分别往右边移动m和n,问能否相遇? [思路] 由题意,可得: X+ ...

  4. Kali2.0通过xrdp实现windows远程链接Linux

    标题:Kali2.0通过xrdp实现windows远程链接Linux apt-get install xrdp 首先需要安装xrdp 接下来安装xfce4 apt-get install xfce4 ...

  5. 【POJ】1840:Eqs【哈希表】

    Eqs Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 18299   Accepted: 8933 Description ...

  6. linux命令-每天一点进步

    2018-05-28 1.yum install -y,这里的-y表示,在安装软件的过程中,无需用户输入yes or no,默认yes 2../sbin/nginx -s reload,重启nginx ...

  7. 2013-8-6 10:56:07 JAVA_WEB:员工号自动生成源代码

    create table user_info_temp (       usId varchar2(20),       usNo varchar2(20),       usName varchar ...

  8. Oracle中空值与数字相加问题

    select 10 + 10 + 10 from dual 结果是30,全然没问题. select null + 10 + 10 from dual 结果是空串,但期望的结果是20. select n ...

  9. datagrid在MVC中的运用04-同时添加搜索和操作区域

    本文介绍在datagrid上同时添加搜索和操作区域. 仅仅是增加操作区域 □ 方法1 $('#dg').datagrid({ toolbar: '#tb' }); <div id="t ...

  10. mysql 主从复制原理(转)

    本文转自https://blog.csdn.net/php_younger/article/details/59673879 mysql 主从复制原理 主从形式   mysql主从复制 灵活 一主一从 ...