pascal’s triangle


Given numRows, generate the first numRows of Pascal’s triangle.

For example, given numRows = 5,
Return

1
2
3
4
5
6
7
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> triangle = new ArrayList<List<Integer>>();
List<Integer> row = new ArrayList<Integer>();
for(int i=0;i<numRows;i++) {
row.add(0,1);
for(int j=1; j<row.size()-1;j++) {
row.set(j,row.get(j)+row.get(j+1));
}
triangle.add(new ArrayList<Integer>(row));
}
return triangle;
}
}