参数的排列组合3

本文介绍了一种通过递归实现的排列组合算法,该算法能够枚举出所有可能的组合方式,并提供了一个完整的Java代码实现。文章还解释了如何对生成的组合进行排序,以及如何计算特定数量参数的组合总数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如何获取排列组合的所有值呢?
之前咱们是求排列组合的取值个数,现在要求取值
首先我们考虑常规的情况:
我们有n个盒子,分别放置n个元素
第一回:我们从n个里面选择一个:有n种可能
第二回:我们从n-1个里面选择一个:有n-1种可能
第三回:我们从n-2个里面选择一个:有n-2种可能
第四回:我们从n-3个里面选择一个:有n-3种可能
……
最后我们只有一个可选
排列

直接上代码:

/***
     * @param base      :[a,b,c,d]
     * @param times
     * @param remaining : 剩余要选择的个数
     * @return
     */
    public static void assemble(List<String> result, StringBuffer buffer, String base[], int times, int remaining, boolean isSort) {
        if (remaining <= 1) {
            buffer.append(base[base.length - 1]);
            addElementBySort(result, buffer, isSort);
        } else {
            for (int i = 0; i < remaining; i++) {
                StringBuffer bufferTmp = new StringBuffer(buffer);
                bufferTmp.append(base[base.length - 1 - i]);
                addElementBySort(result, bufferTmp, isSort);
                assemble(result, bufferTmp, SystemHWUtil.aheadElement(base, base.length - 1 - i), times, remaining - 1, isSort);
            }
        }

    }

参数说明:

参数名含义举例
base样本[a,b,c,d]
remaining剩余可选择的样本个数
times组合的个数目前预留

递归
工具类完整代码:

package com.common.util;

import com.string.widget.util.ValueWidget;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by huangweii on 2016/1/23.
 */
public class AssembleUtil {
    /***
     * @param base      :[a,b,c,d]
     * @param times
     * @param remaining : 剩余要选择的个数
     * @return
     */
    public static void assemble(List<String> result, StringBuffer buffer, String base[], int times, int remaining, boolean isSort) {
        if (remaining <= 1) {
            buffer.append(base[base.length - 1]);
            addElementBySort(result, buffer, isSort);
        } else {
            for (int i = 0; i < remaining; i++) {
                StringBuffer bufferTmp = new StringBuffer(buffer);
                bufferTmp.append(base[base.length - 1 - i]);
                addElementBySort(result, bufferTmp, isSort);
                assemble(result, bufferTmp, SystemHWUtil.aheadElement(base, base.length - 1 - i), times, remaining - 1, isSort);
            }
        }

    }

    /***
     * @param base
     * @param times
     * @param isSort    : 是否对"acb"进行排序,<br />排序结果:"abc"
     * @return
     */
    public static List<String> assemble(String base[], int times,  boolean isSort) {
//        Set<String> result = new HashSet<String>();
        List<String> result = new ArrayList<String>();
        AssembleUtil.assemble(result, new StringBuffer(), base, 0, base.length, true);
        return result;
    }

    public static void addElementBySort(List<String> result, StringBuffer buffer, boolean isSort) {
        String str = buffer.toString();
        if (isSort) {
            str = ValueWidget.sortStr(str);
        }
        if (result.size() == 0 || (!result.contains(str))) {
            result.add(str);
        }
    }

    /***
     * 参数的取值个数,ab和ba算一种
     *
     * @param argCount
     * @return
     */
    public static int getAssembleSum(int argCount) {
        int sum = 0;
        for (int i = 0; i < argCount; i++) {
            int count = i + 1;//参数组合的个数
            sum += (SystemHWUtil.factorial(argCount, count) / SystemHWUtil.arrayArrange(count));
        }
        return sum;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值