leetcode_118 problem--pascal ' s Triangle (simple math problem)

Source: Internet
Author: User

Pascal ' s TriangleTotal accepted:43845 Total submissions:145271my submissions QuestionSolution

Given numrows, generate the first numrows of Pascal ' s triangle.

For example, given numrows = 5,
Return

[     1], [    1,2,1], [   1,3,3,1], [  1,4,6,4,1]

Hide TagsArrayHas you met this question in a real interview? Yes No

Discuss

This problem is a simple math problem, the number of n+1 line is calculated by the number of rows n, that is, the first last is 1, the other number of n rows by the number of the first and the first i+1 the sum of

#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);//First line Temp2.push_back (1), temp2.push_back (1); Templast.push_back ( TEMP2);//second line 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 problem--pascal ' s Triangle (simple math problem)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.