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] ]
import java.util.*;
public class Solution{
public ArrayList<ArrayList<Integer>> generate(int numRows){
ArrayList<ArrayList<Integer>> result = new ArrayList<>();
if(numRows < 1) return result;
ArrayList<Integer> last = null;
for(int i=0; i<numRows; i++)
{
ArrayList<Integer> res = new ArrayList<>();
if(i > 1) last = result.get(i-1);
for(int j=0; j<=i; j++)
{
if(j==0 || j==i) res.add(1);
else res.add(last.get(j-1) + last.get(j));
}
result.add(res);
}
return result;
}
}