Maths could help you simplify the problem.


  • 0

    This solution is based on some properties of nCr:

    1. C(n, r) = C(n, n - r)
    2. C(n, r) = C(n, r - 1) * (n - r + 1) / r
    3. C(n, 0) = 1

    I used recursive form and long type to prevent some overflow problems.

    Ref: https://en.wikipedia.org/wiki/Combination#Number_of_k-combinations

    public class Solution {
        public List<Integer> getRow(int rowIndex) {
            List<Integer> res = new ArrayList();
            if (rowIndex < 0)
                return res;
            for (int i = 0; i <= rowIndex; i++) {
                res.add((int)combN(rowIndex, i));
            }
            return res;
        }
        private long combN(int n, int r) {
            if (r > n / 2)
                return combN(n, n - r);
            if (r == 0)
                return 1;
            return combN(n, r - 1) * (n - r + 1) / r;
        }
    }
    

Log in to reply
 

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