Pascal's Triangle leetcode
2016-01-06 20:07
309 查看
Given numRows, generate the first numRows of Pascal's triangle.
For example, given numRows = 5,
Return
Subscribe to see which companies asked this question
帕斯卡三角形,它的值 a[i][j] = a[i-1][j-1] + a[i-1][j]; 注意如果i-1<0,则a[i-1]=0,也就是假设三角形周边的元素都是0
程序中我们可以直接让边缘的数值为1
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 = 0; i < numRows; ++i) { vector<int> row; for (int j = 0; j <= i; ++j) { if (j == 0 || j == i) row.push_back(1); else row.push_back(ret[i - 1][j - 1] + ret[i - 1][j]); } ret.push_back(row); } return ret; }
相关文章推荐
- Sicily 1741. Jaunt around the Zhuhai Campus
- 文章标题
- 画板记录
- 2015年度引进版技术书TOP10
- 【bzoj4028】[HEOI2015]公约数数列 分块
- 第三章 保证数据完整性
- Caffe学习系列(18): 绘制网络模型
- 《实时控制软件设计》第三组第二天工作日志
- Easyui弹出框
- 《Effective java》读书记录-第11条-谨慎地覆盖clone
- HDOJ 2010 水仙花数
- 图形学理论 光照模型
- 解决Ubuntu14.04 LTS中Chrome死机的问题
- 实战中的 Promise 和 Future
- SDL2.0学习笔记——在屏幕上显示一张图片
- 【BS】compilation debug=true targetFramework=4.0 无法识别的属性“targetFramework”
- opencv的实用研究--分析轮廓并寻找边界点
- AdaBoost--从原理到实现
- MFC基础知识
- Python数据类型