Total Accepted: 77202
Total Submissions: 235367
Difficulty: Easy
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>> res =new ArrayList<List<Integer>>(numRows);
if (numRows == 0) return res;
else {
List<Integer> r = new ArrayList<Integer>();
r.add(1);
res.add(r);
for (int i = 1; i < numRows;i++){
List<Integer> rt = new ArrayList<Integer>(i+1);
rt.add(1);
for (int j = 1;j < i;j++) {
rt.add(res.get(i-1).get(j-1)+res.get(i-1).get(j));
}
rt.add(1);
res.add(rt);
}
return res;
}
}
}