Merge pull request #1159 from akiyamaneko/combination_enhanced

基于#1153 重新做了优化,剔除了缓存,直接计算即可
This commit is contained in:
Golden Looly
2020-10-14 11:00:16 +08:00
committed by GitHub

View File

@@ -6,6 +6,7 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
import cn.hutool.core.util.NumberUtil; import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
/** /**
* 组合即C(n, m)<br> * 组合即C(n, m)<br>
@@ -53,11 +54,10 @@ public class Combination implements Serializable {
* @return 组合数 * @return 组合数
*/ */
public static long countAll(int n) { public static long countAll(int n) {
long total = 0; if (n < 0 || n > 63) {
for (int i = 1; i <= n; i++) { throw new IllegalArgumentException(StrUtil.format("countAll must have n >= 0 and n <= 63, but got n={}", n));
total += count(n, i);
} }
return total; return n == 63 ? Long.MAX_VALUE : (1L << n) - 1;
} }
/** /**