JAVA combination number


  • 1
    public List<Integer> getRow(int rowIndex) {
        List<Integer> resultList = new ArrayList<Integer>();
        int col = 0;
        while(col <= rowIndex)
            resultList.add(combination(rowIndex, col++));
        return resultList;
    }
    
    public int combination(int n, int k){
        long res = 1;
        for(int i = 1; i<=k; i++)
            res = res * (n-i+1) / i;
        return (int) res;
    }

  • 0
    S

    Math……nice and clean


Log in to reply
 

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