参数的排列组合2

简介: <div class="markdown_views"><p>参数的排列组合2 <br>参数的取值范围是:[“a”,”b”,”c”,”d”],求其所有的排列组合 <br>先给出答案: <br>共15个,分别是: <br>abc, d, abd, b, c, a, ac, ad, bcd, ab, bc, acd, abcd, bd, cd</p><p>单元测试:<

参数的排列组合2
参数的取值范围是:[“a”,”b”,”c”,”d”],求其所有的排列组合
先给出答案:
共15个,分别是:
abc, d, abd, b, c, a, ac, ad, bcd, ab, bc, acd, abcd, bd, cd

单元测试:

    @Test
    public void test_factorialaa(){
        String base[]=new String[]{"a","b","c","d"};
        Set<String>result=AssembleUtil.assemble( base, 0, base.length,true);
        System.out.println(result.size());
        System.out.println(result);
    }

核心代码:

package com.common.util;

import com.string.widget.util.ValueWidget;

import java.util.HashSet;
import java.util.Set;

/**
 * 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(Set<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 remaining : 剩余要选择的个数
     * @param isSort : 是否对"acb"进行排序,<br />排序结果:"abc"
     * @return
     */
    public static Set<String> assemble( String base[], int times, int remaining, boolean isSort){
        Set<String>result=new HashSet<String>();
        StringBuffer buffer=new StringBuffer();
        AssembleUtil.assemble(result,new StringBuffer(), base, 0, base.length,true);
        return result;
    }
    public static void addElementBySort(Set<String> result, StringBuffer buffer, boolean isSort) {
        String str=buffer.toString();
        if(isSort){
            str= ValueWidget. sortStr(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;
    }

}
相关文章
|
4月前
|
人工智能 算法 数据可视化
【算法训练-数组 五】【数组组合】:下一个排列
【算法训练-数组 五】【数组组合】:下一个排列
23 0
|
存储 算法
一文搞懂全排列、组合、子集问题
Hello,大家好,我是bigsai,long time no see!在刷题和面试过程中,我们经常遇到一些排列组合类的问题,而全排列、组合、子集等问题更是非常经典问题。本篇文章就带你彻底搞懂全排列!
137 0
一文搞懂全排列、组合、子集问题
|
存储 算法
四式解决回溯算法:组合+组合总和
给定两个整数 n 和 k,返回范围 [1, n] 中所有可能的 k 个数的组合。你可以按 任何顺序 返回答案。 给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。
665 3
【组合数学】生成函数 ( 使用生成函数求解不定方程解个数示例 2 | 扩展到整数解 )
【组合数学】生成函数 ( 使用生成函数求解不定方程解个数示例 2 | 扩展到整数解 )
155 0
|
机器学习/深度学习 移动开发
【组合数学】排列组合 ( 集合组合、一一对应模型分析示例 )
【组合数学】排列组合 ( 集合组合、一一对应模型分析示例 )
151 0
|
机器学习/深度学习 移动开发
【组合数学】排列组合 ( 集合排列、分步处理示例 )
【组合数学】排列组合 ( 集合排列、分步处理示例 )
151 0
【组合数学】排列组合 ( 排列组合示例 )
【组合数学】排列组合 ( 排列组合示例 )
213 0
|
机器学习/深度学习 人工智能
【集合论】容斥原理 ( 包含排斥原理 | 示例 )
【集合论】容斥原理 ( 包含排斥原理 | 示例 )
224 0
【组合数学】生成函数 ( 正整数拆分 | 无序不重复拆分示例 )
【组合数学】生成函数 ( 正整数拆分 | 无序不重复拆分示例 )
277 0