My Java solution


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

Log in to reply
 

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