LeetCode(118) Pascal's Triangle
题目
Given numRows, generate the first numRows of Pascal’s triangle.
For example, given numRows = 5,
Return
分析
构建数字金字塔,由上图可以清楚的找到规律。
该题目可用递归实现!
比较简单~
AC代码
class Solution {
public:
vector<vector<int>> generate(int numRows) {
if (numRows == 0)
return vector<vector<int>>();
else if (numRows == 1)
return vector<vector<int>>(1, vector<int>(1, 1));
//存储当前金字塔
vector<vector<int> > ret = generate(numRows - 1);
//计算当前行
vector<int> cur(numRows, 0);
cur[0] = 1;
cur[numRows - 1] = 1;
for (int i = 1; i < numRows - 1; ++i)
{
cur[i] = ret[numRows - 2][i - 1] + ret[numRows - 2][i];
}//for
ret.push_back(cur);
return ret;
}
};
LeetCode(118) Pascal's Triangle的更多相关文章
- 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 [ ...
- 【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(118):杨辉三角
Easy! 题目描述: 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 5 输出: [ [1], [1,1] ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
- LeetCode(113) Path Sum II
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
随机推荐
- python 基础(十一) pickle 序列化
一.pickle序列化的操作 使用说明:可以将数据 转换成2进制 写入到文件中 或者之间返回 做到将数据原样写入 原样取出 import pickle (1) dump 写入文件中 pickle.du ...
- BZOJ1257(数论知识)
感觉做法很神奇……想不到啊qwq 题目: Description 给出正整数n和k,计算j(n, k)=k mod 1 + k mod 2 + k mod 3 + … + k mod n的值 其中k ...
- CVE-2017-3248——WebLogic反序列化漏洞利用工具
著名的web中间件WebLogic被曝出之前的反序列化安全漏洞补丁存在绕过安全风险,用户更新补丁后,仍然存在被绕过成功执行远程命令攻击的情况,安全风险高,Oracle官方及时发布了最新补丁,修复了该漏 ...
- js验证textarea里面是否有换行符
var reg = /[\r\n]+/g;if(reg.test(reason)){ PopAlert("不能有换行符!"); return;}
- Java之final、static关键字及匿名对象
个人通俗理解: 1.final:首先被final修饰的变量就自动变成的不能被修改的常量了.被修饰的类会自动变成太监类,只能有父类,不能有子类:被修饰的方法也不能被子类重写了:被修饰的引用变量值也不能更 ...
- 定时任务-ScheduledExecutorService
创建定时任务线程池的方式 ScheduledExecutorService executorService = Executors.newScheduledThreadPool(4);// 不推荐// ...
- Java GUI设置图标
ImageIcon是Icon接口的一个实现类. ImageIcon类的构造函数: ImageIcon() ImageIcon(String filename) //本地图片文件 ImageIcon ...
- table表格字母无法换行
在项目中,用到的table比较多,本来布局挺好的,后来在td内写入英文字母,整个布局就乱了,会撑的很宽,不换行,后来才知道:一般字母的话会被浏览器默认是一个字符串或者说一个单词,所以不会自动换行. 于 ...
- uvm_reg_item——寄存器模型(五)
uvm_reg_item 扩展自uvm_sequence_item,也就说寄存器模型定义了transaction item. adapter 的作用是把这uvm_reg_item转换成uvm_sequ ...
- 【MYSQL】mysql-5.6.19-win32免安装版本配置方法
[MYSQL]mysql-5.6.19-win32免安装版本配置方法 1.文件下载网站(http://dev.mysql.com/downloads/): 具体下载地址:http://211.136. ...