题目:
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] ]
思路:
用两层循环逐层求值
Code:
public List<List<Integer>> generate(int numRows) {
ArrayList<List<Integer>> al=new ArrayList<List<Integer>>();
if(numRows==0) return al;
ArrayList<Integer> line;
for(int i=0;i<numRows;i++){
line=new ArrayList<Integer>();
for(int j=0;j<=i;j++){
if(j==0 || j==i) line.add(1);
else line.add(al.get(i-1).get(j-1)+al.get(i-1).get(j));
}
al.add(line);
}
return al;
}
备注: