Problem Description:
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 ]]
Problem Analysis:
The data for the nth row is calculated on the basis of line n-1. is an iterative process
Workaround:
//generate the first n rows of the Yang Hui Triangle Public StaticList<list<integer>> Generate (intnumrows) {List<List<Integer>> List =NewArraylist<list<integer>> ();//Define list if(numrows <= 0)returnlist; List<Integer> row =NewArraylist<integer> ();//First lineRow.add (1); List.add (row); for(inti = 2; I <= numrows; i++) {//Generate the following n-1 row data List<Integer> L =NewArraylist<integer> ();//Storing line I dataL.add (1); Row=NewArraylist<integer> (List.get (List.size ()-1));//get previous row of data for(intj = 0; J < Row.size ()-1; J + +){ intE = Row.get (j) + Row.get (j + 1); L.add (e); } l.add (1); List.add (l); } returnlist; }
Pascal ' s Triangle (Leetcode java)