118. Pascal's Triangle
2016-05-07 21:05
253 查看
题目:
Given numRows, generate the first numRows of Pascal's triangle.
For example, given numRows = 5,
Return
题意:
给出层数,生成杨辉三角。
思路:逐层生成
代码:
C++版:4ms
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] ]
题意:
给出层数,生成杨辉三角。
思路:逐层生成
代码:
C++版:4ms
class Solution { public: vector<vector<int>> generate(int numRows) { vector<vector<int>> result(numRows); for(int i=0; i<numRows; i++){ result[i].resize(i+1); result[i][0] = result[i][i] = 1; for(int j=1; j<i; j++){ result[i][j] = result[i-1][j-1] + result[i-1][j]; } } return result; } };
class Solution { //0ms public: vector<vector<int>> generate(int numRows) { vector<vector<int>> result; for(auto i=0; i<numRows; ++i){ result.push_back(vector<int>(i+1, 1)); for(auto j=1; j<i; ++j){ result[i][j] = result[i-1][j-1] + result[i-1][j]; } } return result; } };java版
public class Solution { public List<List<Integer>> generate(int numRows) { List<List<Integer>> result = new ArrayList<List<Integer>>(); List<Integer> row = new ArrayList<Integer>(); for(int i=0; i<numRows; i++){ row.add(0, 1); for(int j=1; j<row.size()-1; j++){ row.set(j, row.get(j)+row.get(j+1)); } result.add(new ArrayList<Integer>(row)); } return result; } }
相关文章推荐
- 7.UI补充/03静态库和内存分析、硬件获取
- 2016sdau课程练习专题三 1010
- 不调用库函数实现 strCpy
- 虚拟机提示:无法打开内核设备“\\.\Global\vmx86”: 系统找不到指定的文件
- HTML/CSS实现下拉菜单
- 语句练习1
- 代码训练营——TreeSet
- Class Methods & Variables
- HDU 4417-Super Mario(划分树-二分查找)
- Class Methods & Variables
- Zookeeper的安装配置
- Linux下用户组、文件权限详解
- HDU 4586 Play the Dice 概率dp+等比数列 级数
- 超链接a提交表单
- Windows驱动开发(7) - DEVICE_OBJECT结构体
- 对照Java学习Swift--构造过程(Initialization)
- MySQL分区表
- 通过直方图比较图像相似度
- 【Android Native Code开发系列】六 一个Native Service的完整示例
- Java线程详解