public class 递归实现排列组合 {
static int len;
public static void listAll(List list,String prefix) {
if (prefix.length()==len) {
System.out.println(prefix);
}
for (int i = 0; i < list.size(); i++) {
List<Character> temp=new LinkedList<Character>(list);
//函数的参数从右边开始解析,因此参数temp是移除后的temp
listAll(temp, prefix+temp.remove(i));
}
}
public static void main(String[] args) {
String string;
Scanner scanner=new Scanner(System.in);
string=scanner.next();
char c[]=string.toCharArray();
len=string.length();
List<Character> candidate=new LinkedList<>();
for (int i = 0; i < len; i++) {
candidate.add(c[i]);
}
listAll(candidate, "");
}
}
递归实现全排列组合(Java实现)
最新推荐文章于 2021-08-04 04:55:36 发布