注:此博客不再更新,所有最新文章将发表在个人独立博客limengting.site。分享技术,记录生活,欢迎大家关注
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]
]
// ArrayList中的set(index, object)和add(index, object)的区别
// set:将原来index位置上的object的替换掉
// add:将原来index位置上的object向后移动
class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> res = new ArrayList<>();
List<Integer> row = new ArrayList<>();
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));
}
res.add(new ArrayList<>(row));
}
return res;
}
}