[leetcode] 3. Pascal's 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]
]
那么这个就很简单了,按照帕斯卡三角也是杨辉三角的定义来就可以了。我的代码如下:
vector<vector<int>> generate(int numRows)
{
vector<vector<int>> Pascal;
vector<int> tmp; if (numRows == 0)
{
return Pascal;
} tmp.push_back(1);
Pascal.push_back(tmp); for (int i = 1; i < numRows; i++)
{
tmp.clear(); tmp.push_back(1);
for (int j = 1; j < i; j++)
{
tmp.push_back(Pascal[i - 1][j - 1] + Pascal[i - 1][j]);
}
tmp.push_back(1);
Pascal.push_back(tmp);
} return Pascal;
}
这个跟上一题有一个区别是在这里认为帕斯卡三角的第0层应该返回[],而上一题中输入numRows==0时,返回的是[1],这个在最开始加一个判断就可以了没必要纠结。
[leetcode] 3. Pascal's Triangle的更多相关文章
- LeetCode 118 Pascal's Triangle
Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...
- [LeetCode] 119. Pascal's Triangle II 杨辉三角 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 解题报告
[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ...
- LeetCode 119. 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 118. Pascal's Triangle (杨辉三角)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- [LeetCode] 119. Pascal's Triangle II 杨辉三角之二
Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note t ...
- leetcode 118. Pascal's Triangle 、119. Pascal's Triangle II 、120. Triangle
118. Pascal's Triangle 第一种解法:比较麻烦 https://leetcode.com/problems/pascals-triangle/discuss/166279/cpp- ...
- 【leetcode】Pascal's Triangle II
题目简述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Retur ...
- 【leetcode】Pascal's Triangle
题目简述: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...
- LeetCode 119 Pascal's Triangle II
Problem: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Ret ...
随机推荐
- requests模块session处理cookie 与基于线程池的数据爬取
引入 有些时候,我们在使用爬虫程序去爬取一些用户相关信息的数据(爬取张三“人人网”个人主页数据)时,如果使用之前requests模块常规操作时,往往达不到我们想要的目的,例如: #!/usr/bin/ ...
- wwwroot文件无读取某个文件夹权限(对IIS_USER增加全部权限)
- GNU Bash Shell 编程图解笔记
bash,Unix shell的一种,在1987年由布莱恩·福克斯为了GNU计划而编写.1989年发布第一个正式版本,原先是计划用在GNU操作系统上,但能运行于大多数类Unix系统的操作系统之上,包括 ...
- xe8 单元别名
xe8 单元别名 Unit scope F2613 Unit 'Graphics' not found. Project>Option>Unit scope names> vcl.I ...
- Shiro的Subject和Sessoin的创建
之前要先了解Session的来源Shiro session和Spring session一样吗? 创建Subject的位置 AbstractShiroFilter . doFilterInternal ...
- 配置git 环境变量
1.从Git官网下载windows版本的git:http://git-scm.com/downloads 2.一般使用默认设置即可:一路next,git安装完毕! 3.但是如果这时你打开windows ...
- ZTree 获取选中的项,jQuery radio的取值与赋值
$("input[name='Sex']:checked").val();//取值 $("input[name='radioName'][value=2]"). ...
- C#获取类里面的所有的方法名称
Type trypInfo = typeof(Program);// Program为类名//获得方法名string str = "包括的方法名列表:\r\n"; MethodIn ...
- 【HDU2825】Wireless Password【AC自动机,状态压缩DP】
题意 题目给出m(m<=10)个单词,每个单词的长度不超过10且仅由小写字母组成,给出一个正整数n(n<=25)和正整数k,问有多少方法可以组成长度为n的文本且最少包含k个给出的单词. 分 ...
- js的事件冒泡和点击其他区域隐藏弹出层
一.前言 在编写页面的时候,我们经常使用到弹出层.对于弹出层,原本的意义就是增加与用户的交互,提升用户的好感度.如果弹出层都没有较好的体验,那何谈通过交互来提升好感... 首先提出几个弹出层的注意点: ...