Java 1ms


  • 0
        public List<List<Integer>> generate(int numRows) {
            List<List<Integer>> lst = new ArrayList<List<Integer>>();  
            List<Integer> prevList = null;
            if (numRows == 0) return lst;
            for (int i = 0; i < numRows; ++i) {
                lst.add(new ArrayList<Integer>());
            }
           
            if (numRows > 0)  {
                lst.get(0).add(1);
                prevList = lst.get(0);
            }
            if (numRows > 1) {
                lst.get(1).add(1);
                lst.get(1).add(1);
                prevList = lst.get(1);
            }
            
            for (int i = 2; i < numRows; i++) {
                lst.get(i).add(1);
                for (int j = 0, k = 1; k < prevList.size(); ++j, ++k) {
                    lst.get(i).add(prevList.get(j) + prevList.get(k));
                }
                lst.get(i).add(1);
                prevList = lst.get(i);
            }
            return (List<List<Integer>>) lst;
        }

Log in to reply
 

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