工信部网站备案名单,外包公司和劳务派遣哪个好一点,前端开发教程,个人网站制作手绘全排列#xff08;permutation#xff09; 排列组合概念 排列#xff0c;就是指从给定n个数的元素中取出指定m个数的元素#xff0c;进行排序 组合#xff0c;则是指从给定n个数的元素中仅仅取出指定m个数的元素#xff0c;不考虑排序 全排列 以数字为例#xff0c;全排…全排列permutation 排列组合概念 排列就是指从给定n个数的元素中取出指定m个数的元素进行排序 组合则是指从给定n个数的元素中仅仅取出指定m个数的元素不考虑排序 全排列 以数字为例全排列就是从“第一个数字”起“每个数字”分别与它“后面的数字”交换复杂度为O(n!) 图示 A依次和BC交换交换一次后不急如AB交换后不急着交换ACtarget后移再依次交换直到target最后一个数时停止输出返回上一步很明显用递归接着做此时注意要把换了的数再还回来 代码 package permutate;public class Common {static void swap(char str[], int a, int b) {if (a b) {return;}char temp str[a];str[a] str[b];str[b] temp;}static void printArr(char str[]) {for (char c : str) {System.out.print(c );}System.out.println();}
}package permutate;public class 图解全排列 {static int count 0;static void permutation(char str[], int t) {if (t str.length - 1) {// 3.停止System.out.print(count : );Common.printArr(str);return;}for (int i t; i str.length; i) {Common.swap(str, t, i);// 2.递归permutation(str, t 1);// 4.返回上层换回来Common.swap(str, t, i);}}public static void main(String[] args) {char str[] new String(ABC).toCharArray();// 1.从0开始permutation(str, 0);}
} 转载于:https://www.cnblogs.com/AndyHoo/p/8045337.html