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 ArrayList<ArrayList<Integer>> generate(int numRows) {
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
if (numRows <= 0) {
return res;
}
ArrayList<Integer> item = new ArrayList<Integer>();
item.add(1);
res.add(new ArrayList<Integer>(item));
item.clear();
for (int i = 2; i <= numRows; i++) {
ArrayList<Integer> pre = res.get(i - 2);
for (int j = 0; j < i; j++) {
if (j - 1 < 0 || j + 1 > i - 1) {
item.add(1);
} else {
item.add(pre.get(j - 1) + pre.get(j));
}
}
res.add(new ArrayList<Integer>(item));
item.clear();
}
return res;
}
}