题目描述
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]
]
两层for循环——ArrayList
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> outList = new ArrayList<List<Integer>>();
if (numRows == 0) return outList;
for(int i = 0; i < numRows; i++){
List<Integer> subList = new ArrayList<Integer>();
//subList.add(0, 1);
for (int j = 0; j <= i; j++){
if (j < i && j >0) {subList.add(j, outList.get(i - 1).get(j) + outList.get(i - 1).get(j - 1));}
else if (j == i || j == 0) subList.add(j, 1);
}
outList.add(subList);
}
return outList;
}