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] ]Array
public static List<List<Integer>> generate(int numRows) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
List<Integer> first = new ArrayList();
if (numRows==0) return result;
first.add(1);
result.add(first);
if (numRows==1) return result;
for (int i = 1; i < numRows; i++) {
ArrayList<Integer> each = new ArrayList();
each.add(1);// 先把第一个元素填充好
for (int j = 1; j < i; j++) {
// 从第二个元素开始,其等于上一个list的第j-1和j元素之和
each.add(result.get(i - 1).get(j - 1)
+result.get(i - 1).get(j));
}
// 第i个元素还是1
each.add(1);
result.add(each);
}
return result;
}
List<List<Integer>> result = new ArrayList<List<Integer>>();
List<Integer> first = new ArrayList();