java全排列通用工具类
发布时间:2020-05-24 22:47:20 所属栏目:Java 来源:互联网
导读:java全排列通用工具类
|
下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。 脚本之家小编现在分享给大家,也给大家做个参考。 全排列处理接口public interface PermutationProcessor<T> {
void process(T[]array);
}
全排列类public final class FullPermutation {
public static <T> void permutate(T a[],PermutationProcessor<T> processor) {
permutate(a,a.length,processor);
}
static <T> void permutate(T a[],int m,int n,PermutationProcessor<T> processor) {
int i;
T t;
if (m < n - 1) {
permutate(a,m + 1,n,processor);
for (i = m + 1; i < n; i++) {
swap(a,m,i);
permutate(a,processor);
swap(a,i);
}
} else {
processor.process(a);
}
}
private static <T> void swap(T[] a,int i) {
T t;
t = a[m];
a[m] = a[i];
a[i] = t;
}
}
[代码]调用示例 public static void main(String[] args) {
Integer[] a={1,2,4};
FullPermutation.permutate(a,new PermutationProcessor<Integer>() {
@Override
public void process(Integer[] array) {
for(int i:array){
System.out.printf("%d ",i);
}
System.out.println();
}
});
}
[代码]运行结果1 2 4 1 4 2 2 1 4 2 4 1 4 2 1 4 1 2 以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。 如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
