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 ...
随机推荐
- ARC 067 E - Grouping
题面在这里! 很显然是个暴力dp. 我们先枚举一下 队伍人数的种类,然后再逆序枚举一下dp数组里的总人数(顺序就会算重),最后枚举一下这种队伍的数量,之后就可以O(1)算方案了. 具体的,O(1)算方 ...
- Lost connection to MySQL server at 'reading initial communication packet', system error: 0的一个解决方案
-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT在icmp之前/etc/init.d/iptables restar ...
- Linux怎么开启ssh
一.查看ssh开启状态 service ssh status 这是已经开启了的状态 二.如果没有开启 键入以下命令开启 service ssh start 三.开启后如果不能利用xshell远程访问 ...
- CVE-2014-4113本地提权测试
CVE-2014-4113本地提权漏洞分析 By Netfairy 前言 2014年10月14日, Crowdstrike和FireEye发表了一篇文章, 描述了一个新的针对Windows的提权漏洞. ...
- [POI2017]Sabotaż
[POI2017]Sabotaż 题目大意: 一棵\(n(n\le5\times10^5)\)个结点的树,初始时有一个未知的黑点,其余全为白点.对于一个点,如果其子树中黑点所占比例超过\(x\),则这 ...
- 【10.6校内测试】【小模拟】【hash+线段树维护覆盖序列】
一开始看到题就果断跳到T2了!!没想到T2才是个大坑,浪费了两个小时QAQ!! 就是一道小模拟,它怎么说就怎么走就好了! 为什么要用这么多感叹号!!因为统计答案要边走边统计!!如果每个数据都扫一遍20 ...
- UVALive 4426 Blast the Enemy! 计算几何求重心
D - Blast the Enemy! Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Subm ...
- 使用NFS启动Tiny4412开发板根文件系统
1.Ubuntu14.04上搭建NFS服务 1.1.安装NFS服务 $ sudo apt-get install nfs-kernel-server //安装NFS服务 1.2 创建Tiny ...
- C++反汇编-虚函数
学无止尽,积土成山,积水成渊-<C++反汇编与逆向分析技术揭秘> 读书笔记 在C++中,使用关键字virtual声明为虚函数. 虚函数地址表(虚表) 定义:当类中定义有虚函数时,编译器会把 ...
- jrebel使用
背景与愿景:开发环境下,tomcat对热布署的支持还不够全面,致使开发人员浪费大量时间在重起服务上.为了提高开发效率,决定引入Jrebel,它对热布署的支持相对比较全面.虽然Jrebel官方号称使用它 ...