輸出
先看代碼吧
import java
import java
import java
import java
/**
*
* @author 趙學慶
*
*/
public class T {
public static List<String> find(List<String> list) {
List<String> rtn = new ArrayList<String>();
String str;
for (int i =
str = list
list
if (list
rtn
} else {
List<String> sList = find(list);
for (String s : sList) {
rtn
if (s
addNumber(str + s);
}
}
}
list
}
return rtn;
}
public static void addNumber(String str) {
if (str
return;
}
set
}
public static Set<String> set = new TreeSet<String>();
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list
list
list
list
list
list
find(list);
System
int cols =
for (String s : set) {
System
if(cols
System
cols =
}
}
}
}
import java
import java
import java
import java
/**
*
* @author 趙學慶
*
*/
public class T {
public static List<String> find(List<String> list) {
List<String> rtn = new ArrayList<String>();
String str;
for (int i =
str = list
list
if (list
rtn
} else {
List<String> sList = find(list);
for (String s : sList) {
rtn
if (s
addNumber(str + s);
}
}
}
list
}
return rtn;
}
public static void addNumber(String str) {
if (str
return;
}
set
}
public static Set<String> set = new TreeSet<String>();
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list
list
list
list
list
list
find(list);
System
int cols =
for (String s : set) {
System
if(cols
System
cols =
}
}
}
}
輸出結果
網友提供的一個比較優化的算法
import java
import java
/**
* 原始版本沒有考慮重復問題
* 此算法的精華就是
* 這樣就產生了所有可能出現的字符串
*
* @author 趙學慶
*/
public class MyTest {
public static void main(String args[]) {
char[] number = new char[] {
perm(number
System
int cols =
for (String s : set) {
System
if (cols
System
cols =
}
}
}
public static void addNumber(String str) {
set
}
public static Set<String> set = new TreeSet<String>();
public static void perm(char[] n
if (beg == end) {
String result = String
if (n[
return;
if (ntains(
return;
addNumber(String
return;
}
for (int i = beg; i <= end; ++i) {
swap(n
perm(n
swap(n
}
}
public static int number =
public static void swap(char[] n
char temp = n[a];
n[a] = n[b];
n[b] = temp;
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26950.html