LeetCode:Pascal's Triangle I II
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) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
vector<vector<int> >res;
if(numRows == )return res;
res.push_back(vector<int>{});
if(numRows == )return res;
vector<int>tmp;
tmp.reserve(numRows);
for(int i = ; i <= numRows; i++)
{
tmp.clear();
tmp.push_back();
for(int j = ; j < i-; j++)
tmp.push_back(res[i-][j-]+res[i-][j]);
tmp.push_back();
res.push_back(tmp);
}
return res;
}
};
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?
分析:每次计算只和上一层有关系,因此只需要一个数组空间就可以 本文地址
class Solution {
public:
vector<int> getRow(int rowIndex) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
vector<int> res(rowIndex+,);
if(rowIndex == )return res;
for(int i = ; i <= rowIndex; i++)
{
int tmp = res[];
for(int j = ; j <= i-; j++)
{
int kk = res[j];
res[j] = tmp+res[j];
tmp = kk;
}
}
return res;
}
};
【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3436562.html
LeetCode:Pascal's Triangle I II的更多相关文章
- 学会从后往前遍历,例 [LeetCode] Pascal's Triangle II,剑指Offer 题4
当我们需要改变数组的值时,如果从前往后遍历,有时会带来很多麻烦,比如需要插入值,导致数组平移,或者新的值覆盖了旧有的值,但旧有的值依然需要被使用.这种情况下,有时仅仅改变一下数组的遍历方向,就会避免这 ...
- [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][JAVA] Pascal's Triangle I, II
Pascal's Triangle: Given numRows, generate the first numRows of Pascal's triangle. For example, give ...
- [leetcode]Pascal's Triangle II @ Python
原题地址:https://oj.leetcode.com/problems/pascals-triangle-ii/ 题意: Given an index k, return the kth row ...
- LeetCode: Pascal's Triangle II 解题报告
Pascal's Triangle II Total Accepted: 19384 Total Submissions: 63446 My Submissions Question Solution ...
- LeetCode Pascal's Triangle && Pascal's Triangle II Python
Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given ...
- LeetCode - Pascal's Triangle II
题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return ...
- 【leetcode】Pascal's Triangle I & II (middle)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- LeetCode——Pascal's Triangle II
Description: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3 ...
随机推荐
- 关于Socket建立长连接遇到的bug信息
下面是本人在Socket连接的开发中遇到的bug总结 1."远程服务器关闭了Socket长连接'的错误信息 2.关于"kCFStreamNetworkServiceTypeVoIP ...
- (转)为什么大公司青睐Java
转自 http://www.zhihu.com/question/25908953/answer/32119971 因为这是一个商业问题,不是技术问题. 我在面试时探讨过这个问题,对方创业期,问我如果 ...
- Effective Java 15 Minimize mutability
Use immutable classes as much as possible instead of mutable classes. Advantage Easy to design, impl ...
- SQL Server 在windows server2008外网服务器远程连接设置
方法如下: 一.为 SQL Server 2005 启用远程连接 1. 单击"开始",依次选择"程序"."Microsoft SQL Server ...
- MySQL中的FEDERATED引擎
首先说明> FEDERATED存储引擎访问在远程数据库的表中的数据,而不是本地的表.这个特性给某些开发应用带来了便利,你可以直接在本地构建一个federated表来连接远程数据表,配置好 ...
- XSLT
一.简介 XSLT 是一种用于将 XML 文档转换为 XHTML 文档或其他 XML 文档的语言. XSL(eXtensible Stylesheet Language) -- 可扩展标记语言,主要用 ...
- Vbox 安装 OS X 10.11
http://bbs.pcbeta.com/viewthread-1635810-1-1.html http://ibiji.org/post/26.html 破解 Vbox 下OS 限制登录 V ...
- matlab中subplot函数的功能
转载自http://wenku.baidu.com/link?url=UkbSbQd3cxpT7sFrDw7_BO8zJDCUvPKrmsrbITk-7n7fP8g0Vhvq3QTC0DrwwrXfa ...
- C# 遍历枚举类
framework 4.0 环境下 方法 定义枚举类 判断枚举类中是否存在,若存在则输出 例子: Defined.QrCode.QrCodeType type;//枚举类 if (!Enum.TryP ...
- 学习嵌入式Linux-选择iTOP-4412开发板
部分视频观看地址: [视频教程]iTOP-4412开发板之学习方法--致初学者 http://v.youku.com/v_show/id_XNzQ5MDA4NzM2.html [视频教程]三星Exyn ...