LeetCode Pascal's Triangle Pascal三角形

题意:给一个数字,返回一个二维数组,包含一个三角形。
思路:n=0、1、2都是特例,特别处理。3行以上的的头尾都是1,其他都是依靠上一行的两个数。具体了解Pascal三角形原理。
class Solution {
public:
vector<vector<int> > generate(int numRows) {
vector<vector<int> > ans;
if(!numRows) return ans;
vector<int> tmp;
tmp.push_back();ans.push_back(tmp);if(numRows==) return ans;
tmp.push_back();ans.push_back(tmp);if(numRows==) return ans;
for(int i=; i<numRows; i++)
{
tmp.clear();
tmp.push_back();
for(int j=; j<i; j++)
{
tmp.push_back( ans[i-][j-]+ans[i-][j] );
}
tmp.push_back();
ans.push_back(tmp);
}
return ans;
}
};
Pascal's Triangle
LeetCode Pascal's Triangle Pascal三角形的更多相关文章
- 【LeetCode】118 & 119 - Pascal's Triangle & Pascal's Triangle II
118 - Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, ...
- 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 ...
- 28. Triangle && Pascal's Triangle && Pascal's Triangle II
Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to a ...
- Pascal's Triangle,Pascal's Triangle II
一.Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, giv ...
- 118 Pascal's Triangle 帕斯卡三角形 杨辉三角形
给定 numRows, 生成帕斯卡三角形的前 numRows 行.例如, 给定 numRows = 5,返回[ [1], [1,1], [1,2,1], [1,3,3,1], [1 ...
- LeetCode OJ:Triangle(三角形)
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- 【LeetCode】118. Pascal's Triangle 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...
- [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 杨辉三角
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
随机推荐
- François Hollande’s&…
EVER since President François Hollande was elected last May, things have not gone right for him. He ...
- 第3章 编写ROS程序-3
1.订阅者程序 我们继续使用 turtlesim 作为测试平台,订阅 turtlesim_node发布的/turtle1/pose 话题. 这一话题的消息描述了海龟的位姿 (位置和朝向) .尽管目前你 ...
- java之Date(日期)、Date格式化、Calendar(日历)
参考http://how2j.cn/k/date/date-date/346.html Date(日期) Date类 注意:是java.util.Date; 而非 java.sql.Date,此类是给 ...
- php中使用mysqli和pdo扩展,测试连接mysql数据库的效率。
<?php /** * 测试pdo和mysqli的连接效率,各连接100次mysql数据库 */ header("Content-type:text/html;charset=utf8 ...
- 2018ACM-ICPC宁夏邀请赛 A-Maximum Element In A Stack(栈内最大值)
Maximum Element In A Stack 20.91% 10000ms 262144K As an ACM-ICPC newbie, Aishah is learning data s ...
- mysql给id生成uuid
mysql中id一般都设为uuid,除了我们在后台用到的uuid利用jpa注解来生成外,其实在mysql中直接也可以生成 直接上代码: 1.mysql中直接使用uuid()函数,可以生成一个随机的uu ...
- JS Guid生成
function numToGuid(uid) { var str = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"; var l = uid.to ...
- pandas基础(1)_Series和DataFrame
1:pandas简介 Python Data Analysis Library 或 pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的.Pandas 纳入了大量库和一些标 ...
- ue4 杂记
c++获取GameMode if(GetWorld()) { auto gamemode = (ASomeGameMode*)GetWorld()->GetAuthGameMode(); } 或 ...
- ==和equals方法
Java程序中测试两个变量时否相等有两种方法: == 和 equals. ==判断 当使用==来判断两个变量是否相等时,如果两个变量是基本类型变量,且都是数字类型(不一定要求数据类型严格相同),则只要 ...