2ms Java Solution. Maybe not the fastest one but easy to understand


  • 0
    Y
    public List<List<Integer>> generate(int numRows) {
        LinkedList<List<Integer>> list = new LinkedList<List<Integer>>();
        if(numRows==0) return list;
        list.add(new LinkedList<Integer>());
        list.get(0).add(1);
        for(int i=1;i<numRows;i++){
            list.add(new LinkedList<Integer>());
            list.get(i).add(1);
            for(int j=1;j<i;j++){
                list.get(i).add(list.get(i-1).get(j-1)+list.get(i-1).get(j));
            }
            list.get(i).add(1);
        }
        return list;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.