Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 可以由字符组成的字符串组合_Java_Combinations - Fatal编程技术网

Java 可以由字符组成的字符串组合

Java 可以由字符组成的字符串组合,java,combinations,Java,Combinations,如何从Java中的字符数组集合中形成不同的字符串组合 例如 'h' , 'e' , 'l' 结果, h e l he eh le el hl lh hel leh lhe hle ehl elh 试着这样做:- void permute( String input) { int inputLength = input.length(); boolean[ ] used = new boolean[ inputLength ]; StringBuffer outputStr

如何从Java中的字符数组集合中形成不同的字符串组合

例如

'h' , 'e' , 'l'
结果,

h
e
l
he
eh
le
el
hl
lh
hel
leh
lhe
hle
ehl    
elh

试着这样做:-

void permute( String input)
{
  int inputLength = input.length();
  boolean[ ] used = new boolean[ inputLength ];
  StringBuffer outputString = new StringBuffer();
  char[ ] in = input.toCharArray( );

  doPermute ( in, outputString, used, inputLength, 0 );

}

  void doPermute ( char[ ] in, StringBuffer outputString, 
                    boolean[ ] used, int inputlength, int level)
  {
     if( level == inputLength) {
     System.out.println ( outputString.toString()); 
     return;
     }

    for( int i = 0; i < inputLength; ++i )
    {       

       if( used[i] ) continue;

       outputString.append( in[i] );      
       used[i] = true;       
       doPermute( in,   outputString, used, length, level + 1 );       
       used[i] = false;       
         outputString.setLength(   outputString.length() - 1 );   
    }
 }
void排列(字符串输入)
{
int inputLength=input.length();
布尔值[]已使用=新布尔值[inputLength];
StringBuffer outputString=新的StringBuffer();
char[]in=input.toCharArray();
doPermute(in,outputString,已用,inputLength,0);
}
void doPermute(中的字符[],StringBuffer输出字符串,
已使用布尔[],int-inputlength,int-level)
{
如果(级别==输入长度){
System.out.println(outputString.toString());
返回;
}
对于(int i=0;i
更好的定义方法是将其视为两个独立的操作

首先生成输入字符串的所有子集(称为
幂集

接下来,您将生成一个具有指定长度的排列列表(给定其中一个子集)

创建一个执行第一个操作的函数,然后将每个输出集作为输入传递给第二个操作,理论上将生成结果

在代码中,按以下方式考虑:

// Assuming command line params are the input to this operation.
public static void main (String[] args) {
    Set <Set <String>> powerSet = powerSet(new HashSet <String> (Arrays.asList(args)));
    for (Set <String> subset : powerSet) {
        // Permutations need order
        printPermutations(new ArrayList <String> (subset));
    }
}
/*
 * Power set can be generated recursively by considering the two cases: when an element exists in the set, and when the element doesn't exist in the set.
 */ 
public Set <Set <String>> powerSet (Set <String> set) {
   Set <Set <String>> powerSet = new HashSet <Set <String>> ((int)Math.pow(2, set.size()));
   if (set.size() == 0) {
      powerSet.add(new HashSet <String> ());
      return powerSet;
   }
   Set <String> inputCopy = new HashSet <String> (set);
   for (String element : set) {
      inputCopy.remove(element);
      Set <Set <String>> powerSetWithoutElement = powerSet ( inputCopy );
      for (Set <String> subPowerSet : powerSetWithoutElement ) {
          Set <String> powerSetWithElement = new HashSet <String> (subPowerSet);
          powerSetWithElement.add ( element );
          powerSet.add ( powerSetWithElement );
      }
      powerSet.addAll ( powerSetWithoutElement );
   }

   return powerSet;
}

public static void printPermutations(List <String> input) {
   printSubPermutation(input, 0, input.size());
}

public static void printSubPermutation(List <String> input, int startIndex, int endIndex) {
    for (int i = startIndex; i < endIndex; i++) {
        // Swap from start to i
        String temp = input.get(startIndex);
        input.set(startIndex, input.get(i));
        input.set(i, temp);
        // This does: abc -> abc, abc -> bac, abc -> cba
        if (i == endIndex - 1) {
            output(input); // you've reached one permutation here
        } else {
            printSubPermutation(input, startIndex + 1, endIndex);
        }
        // swap back
        String temp = input.get(startIndex);
        input.set(startIndex, input.get(i));
        input.set(i, temp);
    }
}

public static void output(List <String> output) {
    for(String out : output) {
        System.out.print(out + " ");
    }
    System.out.println();
}
//假设命令行参数是此操作的输入。
公共静态void main(字符串[]args){
Set powerSet=powerSet(新的HashSet(Arrays.asList(args));
用于(集合子集:功率集){
//排列需要顺序
打印置换(新数组列表(子集));
}
}
/*
*通过考虑两种情况,可以递归生成幂集:当集合中存在元素时,以及当集合中不存在元素时。
*/ 
公用发电机组(机组){
Set powerSet=newhashset((int)Math.pow(2,Set.size());
if(set.size()==0){
add(新的HashSet());
返回动力装置;
}
Set inputCopy=新哈希集(Set);
for(字符串元素:set){
输入复制。删除(元素);
设置powernetwithoutelement=powerSet(输入副本);
用于(设置子功率集:powerSetWithoutElement){
Set-powerSetWithElement=新哈希集(子功率集);
powerSetWithElement.add(元素);
powerSet.add(powernetwithelement);
}
powerSet.addAll(powernetwithoutelement);
}
返回动力装置;
}
公共静态无效打印置换(列表输入){
printsubermutation(input,0,input.size());
}
公共静态void打印子项(列表输入、int startIndex、int endIndex){
对于(int i=startIndex;iabc,abc->bac,abc->cba
如果(i==endIndex-1){
输出(输入);//您在这里达到了一个排列
}否则{
打印子项(输入,开始索引+1,结束索引);
}
//调回
字符串temp=input.get(startIndex);
set(startIndex,input.get(i));
输入设置(i,温度);
}
}
公共静态无效输出(列表输出){
for(字符串输出:输出){
系统输出打印(输出+“”);
}
System.out.println();
}

正如您所看到的,这种方法虽然理论上正确,但可能不是最好的方法,因为它使用大量堆空间和堆栈空间递归生成powerset。另一方面,排列的生成并没有那么困难。

使用树。阅读本文:


我认为没有任何图书馆具有这种功能。你自己写代码有什么意义?那么你试过了什么?这会不会产生一个类似于他所寻找的结果,即他从char数组['h''e''l']中得到一个'h'作为可能的输出?