经典算法-全排列问题

问题:输出1,2,3三个数的全排列
import java.util.ArrayList;
public class Main {
static ArrayList l=new ArrayList();
//用于检查是否出现过
static boolean []b=new boolean[3];
//回溯
static void f(int []a,int end){//
//一个终结条件,也就是满足条件的时候
if (end==a.length) {
System.out.println(l);
}else {
//循环的数量
for (int i = 0; i < a.length; i++) {
//判断是否用过
if (!b[i]) {
//记录
l.add(a[i]);
//标记用过
b[i]=true;
//进入下一步
f(a,end+1);
//恢复:保存结果之前的状态{回溯一步}
l.remove(l.size()-1);
b[i]=false;
}
}
}

}
public static void main(String[] args){
//循环的数字
int[] a={1,2,3};
f(a,0);
}
}

发布了3 篇原创文章 · 获赞 0 · 访问量 14

猜你喜欢

转载自blog.csdn.net/PROBIE_/article/details/104413135
今日推荐