Java Solution Using Math


  • 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> row = new ArrayList<>();
    row.add(1);
    for(int j = 0; j < i; j++) {
    row.add(row.get(j) * (i-j)/(j+1));
    }
    res.add(row);
    }
    return res;
    }
    }

    This is because C(n,k+1) = C(n,k) * (n-k) / (k+1)


Log in to reply
 

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