2012-12-21 26 views
0

如何可以形成從集字符數組中的Java組合,可以從字符

對於例如串的不同組合來形成,在

'h' , 'e' , 'l' 

結果,

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

我不認爲有這樣的功能的任何庫。你自己寫代碼是什麼? – bsiamionau

+2

那麼你試過了什麼? – vels4j

回答

0

嘗試類似這樣: -

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); 
    } 
} 
+0

這是否會帶來類似結果什麼他尋找,在那裏,他從字符數組[「H」「E」「L」]獲得只是一個「H」爲可能的輸出? –

0

定義這一點的更好方法是將其視爲兩個單獨的操作。

首先要生成輸入字符串(這是調用power set

接下來,要生成的排列的列表與所給出的那些子集之一指定長度的所有子集。

創建執行所述第一操作的功能,然後傳遞每個outputed設置爲輸入到所述第二操作將理論上產生的結果。

在代碼中,把它按以下方式:

// 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(); 
} 

正如你所看到的,這種方法雖然理論上正確,也可能不是最好的方法,因爲它使用了大量的堆空間和堆棧空間遞歸地生成powerset。另一方面,排列的生成並不困難。