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] ]
1 public class Solution { 2 public ArrayList<ArrayList<Integer>> generate(int numRows) { 3 ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); 4 List<Integer> item; 5 if (numRows <= 0) return result; 6 for (int i=0; i<numRows; i++) { 7 item = new ArrayList<Integer>(); 8 for (int j=0; j<=i; j++) { 9 if (j==0 || j==i) item.add(1); 10 else { 11 List<Integer> prev = result.get(i-1); 12 item.add(prev.get(j-1) + prev.get(j)); 13 } 14 } 15 result.add(new ArrayList<Integer>(item)); 16 } 17 return result; 18 } 19 }