package cn.hncu.search.dfs;
public class dfs {
public static void main(String[] args) {
char[] chs={'a','b','c','d'};
int start=0;
int end=chs.length-1;
dfs(chs,start,end);
}
private static void dfs(char[] chs, int start, int end) {
if(start==end+1){
System.out.println(chs);
}else{
for(int i=start;i<=end;i++){
swap(chs, start, i);
dfs(chs, i+1, end);
swap(chs, start, i);
}
}
}
private static void swap(char[] chs, int start, int i) {
char temp=chs[start];
chs[start]=chs[i];
chs[i]=temp;
}
}
全排列
最新推荐文章于 2025-03-10 22:27:24 发布
本文介绍了一个使用Java实现的深度优先搜索(DFS)算法示例。该算法应用于字符数组,通过递归调用实现全排列。代码展示了如何定义递归函数、交换数组元素以及打印结果。
655

被折叠的 条评论
为什么被折叠?



