Java binomial coefficient method


  • 0
    S
    public List<List<Integer>> generate(int numRows) {
            List<List<Integer>> result = new LinkedList<>();
            for (int i = 0; i < numRows; i++) {
                List<Integer> row = new ArrayList<>();
                int s = 1;
                for (int j = 0; j <= i; j++) {
                    row.add(s);
                    s = s * (i - j) / (j + 1);
                }
                result.add(row);
            }
            return result;
    }
    

    Just use the formula of binomial coefficient nCr+1 = nCr * (n -r) / (r + 1)


Log in to reply
 

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