Pascal's Triangle
2014-03-03 17:13
134 查看
Given numRows, generate the first numRows of Pascal's triangle.
For example, given numRows = 5,
Return
For example, given numRows = 5,
Return
[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 模拟由金子塔型换成下三角型会看的清楚一些,注意numRows = 1,2的特殊情况。总体来说简单。
class Solution { public: vector<vector<int> > generate(int numRows) { vector<vector<int> > re; for(int i = 1 ; i <= numRows ; i++) { vector<int> vec; vec.push_back(1); int j; if(i == 1){ re.push_back(vec); continue; } if(i == 2){ vec.push_back(1); re.push_back(vec); continue; } for(j = 1 ; j < i-1; j++)vec.push_back(re[i-2][j-1]+re[i-2][j]); vec.push_back(1); re.push_back(vec); } return re; } };
相关文章推荐
- LeetCode_Pascal's Triangle
- [LeetCode] Pascal's Triangle
- leetcode -- Pascal's Triangle
- LeetCode#118 Pascal's Triangle
- [Array] Pascal's Triangle
- LeetCode OJ——Pascal's Triangle
- [LeetCode]118 Pascal's Triangle
- [LeetCode]题解(python):118-Pascal's Triangle
- [LeetCode 题解]: Pascal's Triangle
- Pascal's Triangle
- Pascal's Triangle
- [leetcode.com]算法题目 - Pascal's Triangle
- Leetcode: Pascal's Triangle
- 119 Pascal's Triangle
- LeetCode(30)-Pascal's Triangle
- Pascal's Triangle
- Pascal's Triangle
- leetcode 生成杨辉三角形, 118 119 Pascal's Triangle 1,2
- LeetCode - Refresh - Pascal's Triangle
- LeetCode——Pascal's Triangle