leetcode 118
118. 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]
] 输出Pascal三角形的前n行;每次利用前面已经生成的行来生成下一行。 代码如下:
class Solution {
public:
vector<vector<int>> generate(int numRows) {
vector<vector<int>> pascal;
vector<int> ss;
if(numRows == )
{
return pascal;
}
if(numRows == )
{
ss.push_back();
pascal.push_back(ss);
return pascal;
}
pascal.push_back({});
for(int i = ; i <numRows; i++)
{
for(int j = ; j <= i; j++)
{
if(j == || j == i)
{
ss.push_back();
continue;
}
int n = pascal[i-][j-] + pascal[i-][j];
ss.push_back(n);
}
pascal.push_back(ss);
ss.clear();
}
return pascal;
}
};
leetcode 118的更多相关文章
- LeetCode 118. 杨辉三角
118. 杨辉三角 给定一个非负整数numRows,生成杨辉三角的前numRows行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例 输入: 5 输出: [ [1], [1,1], [1,2 ...
- 2017-3-6 leetcode 118 169 189
今天什么都没发生 ================================================= leetcode118 https://leetcode.com/problems ...
- 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 118 Pascal's Triangle
Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...
- LeetCode 118. Pascal's Triangle (杨辉三角)
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- Leetcode#118. Pascal's Triangle(杨辉三角)
题目描述 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], [1,2, ...
- LN : leetcode 118 Pascal's Triangle
lc 118 Pascal's Triangle 118 Pascal's Triangle Given numRows, generate the first numRows of Pascal's ...
- LeetCode 118:杨辉三角 II Pascal's Triangle II
公众号:爱写bug(ID:icodebugs) 作者:爱写bug 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行. Given a non-negative index k whe ...
- Java实现 LeetCode 118 杨辉三角
118. 杨辉三角 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1], ...
随机推荐
- Python补充03 Python内置函数清单
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明. Python内置(built-in)函数随着python解释器的运行而创建.在Pytho ...
- bootstrap小例子等
一个简单的表单样式: <div class="row"> <form action="#" class="form-horizont ...
- KVC , KVO , KVB
来源:http://www.cnblogs.com/jay-dong/archive/2012/12/13/2815778.html 熟悉oc语法的同学也许都会懂得这么一点:在oc中,类的成员变量或是 ...
- ruby字符串学习笔记4
1 单独处理字符串的字符 如果处理的是ASCII码的文档使用string#each_byte 注意 没有 string#each方法,String#each_byte 速度比 String#scan快 ...
- NAND FLASH均衡算法笔记(转)
转来一篇关于NAND FLASH均衡算法的文章,加上一点思考和笔记,认为这种思考有助于更深刻的理解,更好的记忆,所以也算半原创了吧,最起码笔记是原创的.有意思的是,帖子提起这个算法并不是因为嵌入式开发 ...
- 反人类的MyEclipse之-MyEclipse设置Console字体大小
Windows-->Preference-->General-->Apperence-->Colors and Fonts --> Debug -->Console ...
- turing 项目引用
1.友盟自动更新 2.友盟统计 3.友盟消息推送 http://www.bejson.com/json2javapojo/ 引用bejson 解析JSON生成类,数组 private List< ...
- Oracle常用命令1
一. 安装是用户管理: sqlplus /nolog; connect /as sysdba; alter user sys identified by change_on_install; alte ...
- (转)C#调用非托管Win 32 DLL
转载学习收藏,原文地址http://www.cnblogs.com/mywebname/articles/2291876.html 背景 在项目过程中,有时候你需要调用非C#编写的DLL文件,尤其在使 ...
- I/B/P SP/SI
H264中I.B.P帧 I帧:只作为参考帧,采用帧内预测 B帧:以其前面的I帧或P帧和后面的P帧作为参考帧 P帧:只能以前面的I帧或P帧作为参考帧 H264中的SP SI SP和SI是H264中引入的 ...