118. Pascal's Triangle

    xiaoxiao2021-03-25  198

    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] ]

    Subscribe to see which companies asked this question.

    public class Solution { public List<List<Integer>> generate(int numRows) { List<List<Integer>> re=new ArrayList<List<Integer>>(); if(numRows==0) return re; List<Integer> list=new ArrayList<Integer>(); list.add(1); re.add(list); for(int i=1;i<numRows;++i){ List<Integer> temp=new ArrayList<Integer>(); temp.add(1); for(int j=1;j<i;++j) temp.add(re.get(i-1).get(j-1)+re.get(i-1).get(j)); temp.add(1); re.add(temp); } return re; } }

    转载请注明原文地址: https://ju.6miu.com/read-2662.html

    最新回复(0)