Leetcode 118 Pascal's Triangle

来源:互联网 发布:软件技术创新点怎么写 编辑:程序博客网 时间:2024/05/21 11:21

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>> result = new ArrayList<List<Integer>>();        ArrayList<Integer> sublist = new ArrayList<Integer>();                for(int i =0 ;i<numRows;i++){            sublist.add(0,1);//add是向list里面添加一个元素 指定位子在第0位 原有的元素座位就往后移动            for(int j =1; j< sublist.size()-1;j++)//其实就是每次添加一个1 然后在使用set改值               sublist.set(j,sublist.get(j+1)+sublist.get(j));                              result.add(new ArrayList<Integer>(sublist));        }        return result;    }}



0 0