[3]118. Pascal's Triangle(Java)

来源:互联网 发布:c语言temp是什么意思 编辑:程序博客网 时间:2024/06/18 06:25

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;    }}
原创粉丝点击