-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
65 changes: 65 additions & 0 deletions
65
data-structure-and-algorithms/src/main/java/com/github/liaojiacan/combine/Combiner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} | ||
|
||
} |
7eb912f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#1