diff --git a/data-structure-and-algorithms/README.md b/data-structure-and-algorithms/README.md index 96df85b..108d1e8 100644 --- a/data-structure-and-algorithms/README.md +++ b/data-structure-and-algorithms/README.md @@ -1,3 +1,4 @@ # 列表 - [1~99,找重复数](https://github.com/liaojiacan/code-snippets/blob/master/data-structure-and-algorithms/src/main/java/com/github/liaojiacan/search/FindDuplicateNum.java) - [判断一个数是否是2的N次方](https://github.com/liaojiacan/code-snippets/blob/master/data-structure-and-algorithms/src/main/java/com/github/liaojiacan/LittleAlgorithms/CheckIsNthPowerOf2.java) + - [对一个列表求C(m,n)的所有组合数组](https://github.com/liaojiacan/code-snippets/blob/master/data-structure-and-algorithms/src/main/java/com/github/liaojiacan/combiner/Combiner.java) diff --git a/data-structure-and-algorithms/src/main/java/com/github/liaojiacan/combine/Combiner.java b/data-structure-and-algorithms/src/main/java/com/github/liaojiacan/combine/Combiner.java new file mode 100644 index 0000000..49d6321 --- /dev/null +++ b/data-structure-and-algorithms/src/main/java/com/github/liaojiacan/combine/Combiner.java @@ -0,0 +1,65 @@ +package com.github.liaojiacan.combine; + +import com.sun.tools.javac.util.Assert; + +import java.util.ArrayList; +import java.util.List; + +/** + * 列表的组合 + * @see leetcode-Combinations + * @author liaojiacan + * @date 2019/1/9 + */ +public class Combiner { + + /** + * 获取originList的 大小为combinationSize的T元素的所有组合 + * @param originList + * @param combinationSize + * @param + * @return + */ + public static List> combine(List originList, int combinationSize){ + Assert.check(originList.size() >=combinationSize,"originList size should greater than combinationSize"); + if(originList.size() == combinationSize ){ + List> result = new ArrayList<>(1); + result.add(originList); + return result; + } + List> combinations = new ArrayList<>(calculateCombinationNum(originList.size(),combinationSize)); + combine(combinations,originList,combinationSize,0,new ArrayList<>(combinationSize)); + return combinations; + } + + + private static void combine(List> combinations,List originList,int combinationSize,int elementIndex,List combination){ + + if( combinationSize == 0 ){ + combinations.add(new ArrayList<>(combination)); + return; + } + + for( int i = elementIndex ; i <= originList.size()-combinationSize ; i++ ){ + combination.add(originList.get(i)); + combine(combinations,originList,combinationSize-1,i+1,combination); + combination.remove(combination.size()-1); + } + + } + + /** + * 计算组合的近视值 C(m,n) + * @see stirling + * @see 组合的近似值算法 + * @param originElementSize + * @param combinationSize + * @return + */ + private static int calculateCombinationNum(int originElementSize,int combinationSize){ + int m = originElementSize; + int n = combinationSize; + return (int) Math.ceil((1/Math.sqrt(2*Math.PI))*Math.sqrt(m/(n*(m-n)))*(Math.pow(m,m)/Math.pow(n,n))*Math.pow((m-n),(n-m))); + } + +}