Leetcode_c++: Pascal's Triangle(118)
2016-06-10 22:26
357 查看
题目
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]
]
算法
O(N^2)每一层的第i个位置,等于上一层第i-1与第i个位置之和。
class Solution { public: vector<vector<int> > generate(int numRows) { vector<vector<int> > ans(numRows); if(numRows==0) return ans; for(int i=0;i<numRows;i++){ ans[i].push_back(1); for(int j=1;j<i;j++) ans[i].push_back(ans[i-1][j-1]+ans[i-1][j]); if(i) ans[i].push_back(1); } return ans; } };
相关文章推荐
- C++中的const用法
- Leetcode_c++:Construct Binary Tree from Inorder and Postorder Traversal (106)
- SCPPO(三):禅道的使用—项目管理员、开发等人员
- 指针和引用
- win7旗舰版安装VC++6.0番茄插件和行号显示
- C++ 删除指定路径文件夹
- C语言选择排序详解及其实现
- C++解压zip文件
- C++11中的std::bind
- SCPPO(二):禅道的使用—管理员
- 【C语言】15-预处理指令1-宏定义
- C++中常用的STL 函数
- C++11多线程之yield
- 在C/C++代码中使用SSE等指令集的指令(1)介绍
- C++ 类的6个缺省函数
- C++Primer 中文版 第五版 第十章课后习题答案
- 【每天学一点】C++基础02
- Balanced Binary Tree
- C/C++杂记:运行时类型识别(RTTI)与动态类型转换原理
- 指针详解与内存映像图(一)