A clean and simple Java solution


  • 0
    L
    public List<List<Integer>> generate(int numRows) {
            List<List<Integer>> lists = new ArrayList<>(numRows);
            if(numRows<=0) {
                return lists;
            }
    
            List<Integer> first = new ArrayList<>();
            for(int i=0;i<numRows;i++) {
                List<Integer> second = new ArrayList<>();
                for(int j=0;j<=i;j++) {
                    if(j==0 || j==i) {
                        second.add(1);
                    }  
                    else {
                        second.add(first.get(j-1) + first.get(j));
                    }
                }
                lists.add(second);
                first = second;
            }
            return lists;
        }
    

Log in to reply
 

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