1ms Java Easy Solution


  • 0
    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> l = new ArrayList<List<Integer>>();
        for(int i=1; i<=numRows; i++) {
            List<Integer> subL = new ArrayList<Integer>();
            if(i==1) {
                subL.add(1);
                l.add(subL);
            } else {
                List<Integer> lastL = l.get(i-2);
                subL.add(1);
                for(int j=1; j<lastL.size(); j++)
                    subL.add(lastL.get(j-1)+lastL.get(j));
                subL.add(1);
                l.add(subL);
            }
        }
        return l;
    }

Log in to reply
 

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