OJ Practice 28--t118 Pascal's Triangle

Source: Internet
Author: User

Print Yang Hui triangle:

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

Enter the number of lines printed and return the vector<vector<int> > type.

Ideas

1. My idea is to allocate vector space first, use resize, and then use subscript to access [i].

2. The other person's idea, each line is a new vector<int> cur, assigns the value after push_back to the RET.

"Other Codes"

vector<vector<int> > Generate (intnumrows) {        intI, J;         for(i=0; i<numrows; i++) {vector<int>cur; if(i==0) Cur.push_back (1);            Else{             for(j=0; j<=i; J + +)            {                if(j==0|| j==i) Cur.push_back (1); Else{                    intans=ret[i-1][j-1]+ret[i-1][j];                Cur.push_back (ANS); }}} ret.push_back (cur); }        returnret; }

"My Code"

vector<vector<int> > Generate (intnumrows) {Vector<vector<int> >ret;
if (numrows==0)
return ret; intI, J; Ret.resize (NumRows); ret[0].push_back (1); for(i=1; i<numrows; i++) {ret[i].resize (i+1); ret[i][0]=1; Ret[i][i]=1; for(j=1; j<i; J + +) {Ret[i][j]=ret[i-1][j-1]+ret[i-1][j]; } } returnret;}

Evaluation

It always fails at first, showing that the last Test executed was {0},

Add if (numrows==0) return ret; this sentence is OK, should consider the illegal situation, so that the return of RET is an empty vector?!

OJ Practice 28--t118 Pascal's Triangle

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.