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 List<List<Integer>> generate(int numRows) {
List<List<Integer>> rec = new ArrayList<List<Integer>>(numRows);
if(numRows <= 0)
return rec;
for(int i=0;i<numRows;i++)
{
List<Integer> arr = new ArrayList<Integer>(i);
for(int j=0;j<=i;j++)
{
if(j==0)
arr.add(1);
else if(j>0&&j<i)
{
int t = rec.get(i-1).get(j-1) + rec.get(i-1).get(j);
arr.add(t);
}
else
arr.add(1);
}
rec.add(arr);
}
return rec;
}
}
网友评论