Java solution


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

Log in to reply
 

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