参数的排列组合3

简介: <div class="markdown_views"><p>如何获取排列组合的所有值呢? <br>之前咱们是求排列组合的取值个数,现在要求取值 <br>首先我们考虑常规的情况: <br>我们有n个盒子,分别放置n个元素 <br>第一回:我们从n个里面选择一个:有n种可能 <br>第二回:我们从n-1个里面选择一个:有n-1种可能 <br>第三回:我们从n-

如何获取排列组合的所有值呢?
之前咱们是求排列组合的取值个数,现在要求取值
首先我们考虑常规的情况:
我们有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;
    }

}
相关文章
|
2月前
|
人工智能 自然语言处理 算法
【动态规划】【字符串】【前缀和】1639通过给定词典构造目标字符串的方案数
【动态规划】【字符串】【前缀和】1639通过给定词典构造目标字符串的方案数
|
存储 算法
四式解决回溯算法:组合+组合总和
给定两个整数 n 和 k,返回范围 [1, n] 中所有可能的 k 个数的组合。你可以按 任何顺序 返回答案。 给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。
665 3
【组合数学】生成函数 ( 使用生成函数求解不定方程解个数示例 2 | 扩展到整数解 )
【组合数学】生成函数 ( 使用生成函数求解不定方程解个数示例 2 | 扩展到整数解 )
155 0
|
机器学习/深度学习 移动开发
【组合数学】排列组合 ( 集合组合、一一对应模型分析示例 )
【组合数学】排列组合 ( 集合组合、一一对应模型分析示例 )
151 0
|
机器学习/深度学习 移动开发
【组合数学】排列组合 ( 集合排列、分步处理示例 )
【组合数学】排列组合 ( 集合排列、分步处理示例 )
151 0
【组合数学】排列组合 ( 排列组合示例 )
【组合数学】排列组合 ( 排列组合示例 )
213 0
【组合数学】生成函数 ( 正整数拆分 | 无序不重复拆分示例 )
【组合数学】生成函数 ( 正整数拆分 | 无序不重复拆分示例 )
277 0
字符串个数匹配问题
# 7-2 子字符串个数匹配 分别输入两个字符串A和B,A由多个小字符串组成(中间由非字母隔开),B是由字母组成的字符串。求出A中包含B的小字符串的个数(详细看样例),并且输出它。(不区分大小写) ### 输入格式: 先输入字符串A,由回车结束。然后输入字符串B。 ### 输出格式: 输出A中包含B字符串的个数、 ### 输入样例: 在这里给出一组输入。例如: ```in aaBbc4./ewfeAbc wefW%!%&aAbc++0 4Abccabc aBc ``` ### 输出样例: 在这里给出相应的输出。例如: ```out 3 ``` 解释: A可以看成:a
470 0