Pascal's Triangle leetcode
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]
]
Subscribe to see which companies asked this question
0 0
0[1]0
0[1 1]0
0[1 2 1]0
0[1 3 3 1]0
0[1 4 6 4 1]
帕斯卡三角形,它的值 a[i][j] = a[i-1][j-1] + a[i-1][j]; 注意如果i-1<0,则a[i-1]=0,也就是假设三角形周边的元素都是0
程序中我们可以直接让边缘的数值为1
vector<vector<int>> generate(int numRows) {
vector<vector<int>> ret;
for (int i = ; i < numRows; ++i)
{
vector<int> row;
for (int j = ; j <= i; ++j)
{
if (j == || j == i)
row.push_back();
else
row.push_back(ret[i - ][j - ] + ret[i - ][j]);
}
ret.push_back(row);
}
return ret;
}
Pascal's Triangle leetcode的更多相关文章
- Pascal's Triangle leetcode java(杨辉三角)
题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, ...
- [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 ...
- LeetCode 118 Pascal's Triangle
Problem: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows ...
- [LeetCode]题解(python):119 Pascal's Triangle II
题目来源 https://leetcode.com/problems/pascals-triangle-ii/ Given an index k, return the kth row of the ...
- [LeetCode]题解(python):118 Pascal's Triangle
题目来源 https://leetcode.com/problems/pascals-triangle/ Given numRows, generate the first numRows of Pa ...
- LeetCode:Pascal's Triangle I II
LeetCode:Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For examp ...
- LeetCode——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 an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...
随机推荐
- enum 用法
public enum WeekDay { SUN(, "Sunday", "SUN"), MON(, "Monday", "MO ...
- 使用 Passenger +Apache扩展 Puppet,代替其Webrick的web框架
使用 Passenger +Apache扩展 Puppet,代替其Webrick的web框架 1安装 yum install ruby-devel ruby-libs rubygems libcurl ...
- 关于JAVA IO流的学习
初学Java,一直搞不懂java里面的io关系,在网上找了很多大多都是给个结构图草草描述也看的不是很懂.而且没有结合到java7 的最新技术,所以自己来整理一下,有错的话请指正,也希望大家提出宝贵意见 ...
- [工作总结] QA小鸟一年了
夏至又至,在北京360的一年过去了.作为一名QA,我时常感到迷惑,如何靠大部分的手工测试+少部分的自动化测试来保证产品功能的质量.对于开发完成后,给到我手上的文件和功能说明,我很少有信心说能够通过自己 ...
- eNSP仿真学习,网络入门!
为了简单的认识Internet的框架的整体结构,简单学习华为的eNSP软件来高度模拟仿真网络框架!(华为和思科公司都发布了自己的网络设备仿真软件,当然我就用国产的吧~) 华为官方的eNSP学习论坛网站 ...
- Java异常处理机制以及try-catch-finally-return执行顺序
一,简单描述: 当出现程序无法控制的外部环境问题(用户提供的文件不存在,文件内容损坏,网络不可用...)时,JAVA就会用异常对象来描述. 二,JAVA中用2种方法处理异常: 1.在发生异常的地方直接 ...
- Python爬虫:通过关键字爬取百度图片
使用工具:Python2.7 点我下载 scrapy框架 sublime text3 一.搭建python(Windows版本) 1.安装python2.7 ---然后在cmd当中输入python,界 ...
- mysql 打开sql日志,记录所有sql
我使用的mysql版本为:5.7.11 win7环境 记录下下载地址,省得每次百度搜了:http://dev.mysql.com/downloads/installer/ mysql 默认没有开启sq ...
- TFS实现需求工作项自动级联保存
目前在一个大型的金融客户软件研发平台项目实施和支持过程中,客户的质量管理团队基于该平台以及结合其它的平台数据,针对需求管理和业务过程需要拟定了一套完整的需求提出.评审.设计以及实现的流程.基于这套流程 ...
- (转)java二维数组的深度学习(静态与动态)
转自:http://developer.51cto.com/art/200906/128274.htm,谢谢 初始化: 1.动态初始化:数组定义与为数组分配空间和赋值的操作分开进行:2.静态初始化:在 ...