Pascal's Triangle I,II
题目来自于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]; }
};
我们必须又一次设计算法。
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时候就报错了
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;
}
};
Pascal's Triangle I,II的更多相关文章
- 【Leetcode】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 ...
- LeetCode——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 ...
- 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 ...
- [LeetCode]Pascal's Triangle II
题目:给定一个行索引index,返回帕斯卡三角形第index层的三角形 算法:生成index层帕斯卡三角形,并返回第index层三角形 public class Solution { public L ...
- LeetCode——Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- leetcode笔记:Pascal's Triangle
一. 题目描写叙述 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...
- leetcode - Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- LeetCode118:Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- 【leetcode】118. Pascal's Triangle
@requires_authorization @author johnsondu @create_time 2015.7.23 19:54 @url [Pascal's Triangle](http ...
随机推荐
- [BalticOI2002]Bicriterial routing
OJ题号: BZOJ1375.ECNU1468 题目大意: 给定一个无向连通图,每条边有两个权值w1和w2.定义一条路径是优秀的当且仅当没有别的路径满足两个权值的和都比该路径小,求s到t的优秀路径条数 ...
- hdu 5288 OO’s Sequence(2015多校第一场第1题)枚举因子
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5288 题意:在闭区间[l,r]内有一个数a[i],a[i]不能整除 除去自身以外的其他的数,f(l,r ...
- python开发_函数的参数传递
在这个用例中,我们要讨论的是关于函数的传参问题 我所使用的python版本为3.3.2 对于函数: def fun(arg): print(arg) def main(): fun('hello,Ho ...
- [Visual Studio] 安装清单
VS安装位置要求路径必须是英文,且位于Program Files (x86)文件夹下. 下载工具vs_Professional.exe:https://pan.baidu.com/s/1jHRjiia ...
- HTML5学习笔记3
7.文档元素 文档元素的主要作用是划分各个不同的内容,让整个页面布局清晰明快,让整个布局具有语义,进一步替代div.基本上没有什么实际作用效果,主要目的是在页面布局时区分各个主题和概念. h1~h6 ...
- POJ 1470 Closest Common Ancestors (LCA, dfs+ST在线算法)
Closest Common Ancestors Time Limit: 2000MS Memory Limit: 10000K Total Submissions: 13370 Accept ...
- IT程序猿们,我该做什么选择呢
这个时刻,我想我遇到人生小拐点了,程序猿到了30岁,到达了一个分界线了,现在的我该何去何从呢? 先谈下简单的情况吧: 来这个公司2年了,之前因为身体的原因,不想那么累,于是选择了一份维护的工作,就来了 ...
- CSS之BFC、IFC、FFC and GFC
CSS之BFC.IFC.FFC and GFC 什么是FC? BFC(Block Formatting Contexts) BFC的布局规则: 如何生成BFC: IFC(Inline Formatti ...
- powerdesigner反向SQLServer2008数据库生成物理数据模型
方法一:通过数据库脚本生成物理数据模型 具体步骤如下图所示:
- MVC使用TempData跨控制器传递信息而无需记住key的名称
通常情况下,使用TempData需要记住key的名称,本篇体验:通过帮助类,实现对TempData的设置.获取.删除. 关于传递信息的类: namespace MvcApplication1.Mode ...