leetcode - Pascal's Triangle

简介:

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]
]

class Solution {
public:
    std::vector<std::vector<int> > generate(int numRows) {
		std::vector<int> vec;
		std::vector<std::vector<int>> res(numRows,vec);
		int triangle[100][100];
		for (int i = 0; i < numRows; i++)
		{
			triangle[i][0] = 1;
			triangle[i][i] = 1;
		}
		for (int i = 2; i < numRows; i++)
		{
			for (int j = 1; j < i + 1; j++)
			{
				triangle[i][j] = triangle[i-1][j-1] + triangle[i-1][j];
			}
		}
		for (int i = 0; i < numRows; i++)
		{
			for (int j = 0; j <= i; j++)
			{
				res[i].push_back(triangle[i][j]);
			}
		}
		return res;
    }
};








本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5076892.html,如需转载请自行联系原作者


相关文章
LeetCode 118:杨辉三角 II Pascal's Triangle II
公众号:爱写bug(ID:icodebugs)作者:爱写bug 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行。 Given a non-negative index k where k ≤ 33, return the kth index row of the Pascal's triangle. Note that the row index starts from 0. 在杨辉三角中,每个数是它左上方和右上方的数的和。
973 0
Leetcode 118:Pascal's Triangle 杨辉三角
118:Pascal's Triangle 杨辉三角 Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。
1044 0
|
Java
LeetCode 118 Pascal's Triangle(帕斯卡三角形)(vector)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50568461 翻译 给定一个行数字,生成它的帕斯卡三角形。
1082 0
|
算法 索引
LeetCode 119 Pascal's Triangle II(帕斯卡三角形II)(vector、数学公式)(*)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/50568802 翻译 给定一个索引K,返回帕斯卡三角形的第K行。
1199 0
[LeetCode]118.Pascal's Triangle
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/SunnyYoona/article/details/43562277 题目 G...
963 0
[LeetCode]119.Pascal's Triangle II
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/SunnyYoona/article/details/43562603 题目 G...
845 0
|
12月前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
175 6
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
132 6
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
291 2