Java readable solution. (nothing fancy about this)


  • 0
    V
    public class Solution {
        public List<List<Integer>> generate(int numRows) {
            List<List<Integer>> pascal = new ArrayList<>();
            for (int i = 0; i < numRows; i++) {
                List<Integer> row = new ArrayList<>();
                row.add(1);
                if (i >= 1) {
                    List<Integer> prevRow = pascal.get(i - 1);
                    
                    for (int j = 1; j <= i - 1; j++) {
                        row.add(prevRow.get(j) + prevRow.get(j - 1));
                    }
                    row.add(1);
                }
                pascal.add(row);
            }
            return pascal;
        }
    }
    

Log in to reply
 

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