118. 119. Pascal's Triangle -- 杨辉三角形
118.
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>> ans;
if(numRows < )
return ans;
vector<int> pre, cur;
pre.push_back();
ans.push_back(pre);
for(int i = ; i < numRows; i++)
{
cur.push_back();
for(int j = ; j < pre.size()-; j++)
{
cur.push_back(pre[j]+pre[j+]);
}
cur.push_back();
ans.push_back(cur);
pre = cur;
cur.clear();
}
return ans;
}
};
119.
Given an index k, return the kth row of the Pascal's triangle.
For example, given k = 3,
Return [1,3,3,1]
.
class Solution {
public:
vector<int> getRow(int rowIndex) {
vector<int> ans(rowIndex+, );
ans[] = ;
for(int i = ; i <= rowIndex; i++)
{
ans[i] = ;
for(int j = i-; j > ; j--)
{
ans[j] += ans[j-];
}
}
return ans;
}
};
只开一个数组,从后向前计算。
118. 119. Pascal's Triangle -- 杨辉三角形的更多相关文章
- 【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, ...
- 118/119. Pascal's Triangle/II
原文题目: 118. Pascal's Triangle 119. Pascal's Triangle II 读题: 杨辉三角问题 '''118''' class Solution(object): ...
- leetcode 生成杨辉三角形, 118 119 Pascal's Triangle 1,2
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- leetcode 生成杨辉三角形, 118 119 Pascal's Triangle 1,2
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Retu ...
- 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 OJ 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, ...
- 118. Pascal's Triangle杨辉三角形(全部/一行)
[抄题]: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...
- [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]题解(python):119 Pascal's Triangle II
题目来源 https://leetcode.com/problems/pascals-triangle-ii/ Given an index k, return the kth row of the ...
随机推荐
- An Example of On-Error Trigger in Oracle Forms
I wrote this trigger around 4 years ago to handle errors in an application based on Oracle Forms 6i. ...
- [CF738D]Sea Battle(贪心)
题目链接:http://codeforces.com/contest/738/problem/D 题意:1*n的格子里有a条长为b的船.有一个人射了k发子弹都没打中船,现在问最少再打多少次一定能保证射 ...
- ADC驱动器或差分放大器设计指南
作为应用工程师,我们经常遇到各种有关差分输入型高速模数转换器(ADC)的驱动问题.事实上,选择正确的ADC驱动器和配置极具挑战性.为了使鲁棒性ADC电路设计多少容易些,我们汇编了一套通用“路障”及解决 ...
- linux下(修改|忘记)mysql密码
好尴尬,经常忘记自己的密码 修改的用户都以root为列.一.拥有原来的myql的root的密码: 方法一:在mysql系统外,使用mysqladmin# mysqladmin -u root -p p ...
- CUBRID学习笔记 8 复制数据库
1 export database 类似sqlserver的分离数据库 cubrid unloaddb demodb分离后生成三个文件 demodb_objects, demodb_indexe ...
- Sublime Text 2 快捷键用法大全(转)
Ctrl+D 选词 (反复按快捷键,即可继续向下同时选中下一个相同的文本进行同时编辑)Ctrl+G 跳转到相应的行Ctrl+J 合并行(已选择需要合并的多行时)Ctrl+L 选择整行(按住-继续选择下 ...
- jQuery.validate.js+API_cn
名称 返回类型 描述 validate(options) 返回:Validator 验证所选的FORM valid() 返回:Boolean 检查是否验证通过 rules() 返回:Options ...
- 表单美化-原生javascript和jQuery多选按钮(兼容IE6)
前些天我们讲了下单选按钮的美化今天来做表单元素多选按钮的美化.我们的想法是:利用多选按钮是否被选中和是否不给选择的特性来为按钮的父元素添加对应的样式,就是说用什么的样式是由按钮的状态来决定. 用到的图 ...
- Nginx基础知识之——配置文件信息(检查配置文件是否正确)
一.检查配置文件是否正确: /usr/local/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf 检查结果: nginx: [emerg] ngin ...
- HDU 5119
被一个学长逼着做的题...谢谢他了~ 题中dp[i][j] i即为第i个数,j为当前输入的数能xor到的数 同时一个数有两种选择,1.not xor 2.xor 最大的j不会超过11...11b( ...