Skip to content

Commit

Permalink
#业务算法# 求一个列表的C(m,n)的所有组合
Browse files Browse the repository at this point in the history
  • Loading branch information
0xbitboy committed Jan 9, 2019
1 parent e96a4c0 commit 7eb912f
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions data-structure-and-algorithms/README.md
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -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 <a href="https://leetcode-cn.com/problems/combinations/submissions/">leetcode-Combinations</a>
* @author liaojiacan
* @date 2019/1/9
*/
public class Combiner {

/**
* 获取originList的 大小为combinationSize的T元素的所有组合
* @param originList
* @param combinationSize
* @param <T>
* @return
*/
public static <T> List<List<T>> combine(List<T> originList, int combinationSize){
Assert.check(originList.size() >=combinationSize,"originList size should greater than combinationSize");
if(originList.size() == combinationSize ){
List<List<T>> result = new ArrayList<>(1);
result.add(originList);
return result;
}
List<List<T>> combinations = new ArrayList<>(calculateCombinationNum(originList.size(),combinationSize));
combine(combinations,originList,combinationSize,0,new ArrayList<>(combinationSize));
return combinations;
}


private static <T> void combine(List<List<T>> combinations,List<T> originList,int combinationSize,int elementIndex,List<T> 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 <a href="https://program-dog.blogspot.com/2017/05/stirlingapproximation.html">stirling</a>
* @see <a href="https://blog.csdn.net/uself/article/details/54575930">组合的近似值算法</a>
* @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)));
}

}

1 comment on commit 7eb912f

@0xbitboy
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#1

Please sign in to comment.