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]
Each layer of Pascal starts and ends at 1 and from the second position its value is the diagonal two number of its upper layer, and with a for loop you can step through this construction code as follows:
public class Solution {public list<list<integer>> generate (int numrows) { list<list< Integer>> res=new arraylist<list<integer>> (); if (numrows==0) return res; List<integer> tmp=new arraylist<integer> (); Tmp.add (1); Res.add (TMP); if (numrows==1) return res; for (int i=1;i<numrows;i++) { list<integer> ntmp=new arraylist<integer> (); Ntmp.add (1); for (int j=0;j<i-1;j++) { ntmp.add (Res.get (i-1). Get (j) +res.get (i-1). Get (J+1)); } Ntmp.add (1); Res.add (ntmp); } return res; }}
Pascal ' s Triangle