java 全排列算法;
发布网友
发布时间:2022-06-06 15:12
我来回答
共1个回答
热心网友
时间:2022-07-12 05:12
= =~思路什么的...用递归吧:
package mon_11;
import java.util.HashSet;
public class ArrangeAll {
private static HashSet<String> set = new HashSet<String>();
public static void arrangeAll(String s) {
put(new StringBuilder(s), new StringBuilder());
}
static void put(StringBuilder s1, StringBuilder s2) {
if (s1.length() == 0)set.add(s2.toString());
for (int i = 0; i < s1.length(); i++) {
put(new StringBuilder(s1).deleteCharAt(i),new StringBuilder(s2).append(s1.charAt(i)));
}
}
public static void main(String[] args) {
arrangeAll("abcd");
System.out.println(set);
}
}
----
输出:
[dcab, acdb, acbd, bcda, bdca, bdac, dbca, bacd, cabd, cdba, cdab, badc, dabc, cadb, dbac, bcad, dacb, cbda, cbad, adbc, adcb, abcd, abdc, dcba]