leetcode_118题——Pascal's Triangle(简单的数学题)

Pascal‘s Triangle

Total Accepted: 43845 Total Submissions: 145271My Submissions

Question Solution

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

Hide Tags

Array

Have you met this question in a real interview?

Yes

No

Discuss

这道题是一个简单的数学题,第n+1行的数由第n行的数来计算,即最前最后为1,其他的数依次由n行的第i和第i+1个数相加而得

#include<iostream>
#include <vector>
using namespace std;

vector<vector<int>> generate(int numRows) {
	vector<int> temp1;
	vector<int> temp2;
	vector<vector<int> >templast;
	if(numRows==0)
		return templast;
	if(numRows==1)
	{
		temp1.push_back(1);
		templast.push_back(temp1);
		return templast;
	}
	if(numRows==2)
	{
		temp1.push_back(1);
		templast.push_back(temp1);
		temp1.push_back(1);
		templast.push_back(temp1);
		return templast;
	}

	temp1.push_back(1);
	templast.push_back(temp1);//第一行
	temp2.push_back(1),temp2.push_back(1);
	templast.push_back(temp2);//第二行

	for(int i=2;i<numRows;i++)
	{
		temp1.clear();
		int len=temp2.size();
		temp1.push_back(1);
		for(int i=0;i<len-1;i++)
			temp1.push_back(temp2[i]+temp2[i+1]);
		temp1.push_back(1);
		templast.push_back(temp1);
		temp2.clear();
		temp2=temp1;
	}
	return templast;
}
int main()
{
	vector<vector<int> > vec1;
	vec1=generate(5);

	int len_n=vec1.size();

	for(int i=0;i<len_n;i++)
	{
		int len_m=vec1[i].size();
		for(int j=0;j<len_m;j++)
			cout<<vec1[i][j]<<‘ ‘;
		cout<<endl;
	}

}

  

leetcode_118题——Pascal's Triangle(简单的数学题)

时间: 2024-08-09 17:41:21

leetcode_118题——Pascal's Triangle(简单的数学题)的相关文章

【leetcode刷题笔记】Pascal&#39;s Triangle II

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? 题解:简单的模拟题,每次用answer列表存放上一层的值,用temp列表存放当前层的值,只要计算好temp中需要重新计算的元素的索引范围[1,i-1]

LeetCode_118. Pascal&#39;s Triangle

118. Pascal's Triangle Easy Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's triangle, each number is the sum of the two numbers directly above it. Example: Input: 5 Output: [ [1], [1,1], [1,2,1], [1,

LeetCode开心刷题五十一天——118. Pascal&#39;s Triangle 接触跳转表概念,不知用处 lamda逗号导致表达式加法奇怪不理解119. Pascal&#39;s Triangle II

118. Pascal's Triangle Easy 87984FavoriteShare Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's triangle, each number is the sum of the two numbers directly above it. Example: Input: 5 Output: [ [1],

LeetCode练题——118. Pascal&#39;s Triangle

1.题目 Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's triangle, each number is the sum of the two numbers directly above it. Example: Input: 5 Output: [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 2

LeetCode: Pascal&#39;s Triangle 解题报告

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] ] SOLUTION 1:很easy的题.注意记得把List加到ret中.比较简单,每一行的每一个元素有这个规律:1. 左右2边的是1.i, j 表示行,列坐标.2.

leetcode -day13 Valid Palindrome &amp; Triangle &amp; Pascal&#39;s Triangle I II

1.  Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example, "A man, a plan, a canal: Panama" is a palindrome. "race a car" is not a palindrome. Note:

[leetcode] 3. Pascal&#39;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] ] 那么这个就很简单了,按照帕斯卡三角也是杨辉三角的定义来就可以了.我的代码如下: vector<vector<int>> gener

Pascal&#39;s Triangle 1 &amp; 2

这两题都比较简单,第一题输出杨辉三角,第二题输出特定的某一行,第二题要求空间复杂度为O(k) 代码如下: Pascal's Triangle: public List<List<Integer>> generate(int numRows) {//direct simulate List<List<Integer>> rs = new LinkedList<List<Integer>>(); if(numRows == 0)retur

28. Triangle &amp;&amp; Pascal&#39;s Triangle &amp;&amp; Pascal&#39;s Triangle II

Triangle Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom