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] ]
public class Solution { public List<List<Integer>> generate(int numRows) { List<List<Integer> > pascalTriangle = new ArrayList<List<Integer> >(); List<Integer> row1 = new ArrayList<Integer>(); row1.add(1); List<Integer> row2 = new ArrayList<Integer>(); row2.add(1); row2.add(1); if(numRows == 1) pascalTriangle.add(row1); if(numRows == 2){ pascalTriangle.add(row1); pascalTriangle.add(row2); } if(numRows > 2){ pascalTriangle.add(row1); pascalTriangle.add(row2); for(int i = 3; i <= numRows; ++i){ List<Integer> upList = pascalTriangle.get(i - 2); List<Integer> currentList = new ArrayList<Integer>(); currentList.add(1); for(int j = 1; j < i - 1; ++j) currentList.add(upList.get(j - 1) + upList.get(j)); currentList.add(1); pascalTriangle.add(currentList); } } return pascalTriangle; } }