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 ...
随机推荐
- CodeForces1070A Find a Number 图论
令状态$f(i, j)$表示模$d$为$i$,和为$j$时的最小数 可以通过$bfs$来转移 然而就没了... 复杂度$O(10ds)$ #include <queue> #include ...
- POJ2104 K-th Number 不带修改的主席树 线段树
http://poj.org/problem?id=2104 给定一个序列,求区间第k小 通过构建可持久化的点,得到线段树左儿子和右儿子的前缀和(前缀是这个序列从左到右意义上的),然后是一个二分的ge ...
- 【dijkstra优化/次短路径】POJ3255-Roadblocks
[题目大意] 给出一张无向图,求出从源点到终点的次短边. [思路] 先来谈谈Dijkstra的优化.对于每次寻找到当前为访问过的点中距离最短的那一个,运用优先队列进行优化,避免全部扫描,每更新一个点的 ...
- 【bzoj2005】 [Noi2010]能量采集 数学结论(gcd)
[bzoj2005] [Noi2010]能量采集 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnli ...
- Jquery ajax传递复杂参数给WebService
参考: http://www.cnblogs.com/kingge/archive/2011/08/04/2127642.html http://www.cnblogs.com/micromouse/ ...
- Windows Phone Silverlight 8.1 apps
The Windows Phone Silverlight 8.1 app model gives Windows Phone 8 developers access to some of the n ...
- 点赞和吐糟Adblock Plus~进阶教程
前言:Adblock Plus后文都简称ABP,这是一篇ABP进阶教程!用ABP实现flashBlock和NoScript.推荐有相当基础的阅读.刚開始学习的人先看懂这里:http://adblock ...
- MVC文件上传02-使用HttpPostedFileBase上传多个文件
本篇体验上传多个文件.兄弟篇为: MVC文件上传01-使用jquery异步上传并客户端验证类型和大小 MVC最基本上传文件方式中的一个遗漏点 □ 前台视图部分 1: <% u ...
- Android中UI设计的一些技巧!!!
出处:http://blog.csdn.net/android_tutor/article/details/5995759 大家好,今天给大家分享的是Android中UI设计的一些技巧,本节内容主要有 ...
- Ext.QuickTips.init()的使用
在extJS的例子中,大部分都在程序第一行使用了如下语句:Ext.QuickTips.init();但是QuickTips的用处是什么呢?我们看一段最简单的代码: <html> <h ...