排列组合问题

public class CommonTest {

//    public static void main(String[] args) {
//        perm(new int[]{1,2,3},new Stack<>());
//    }
//    public static void perm(int[] array, Stack<Integer> stack) {
//        if(array.length <= 0) {
//            //进入了叶子节点,输出栈中内容
//            System.out.println(stack);
//        } else {
//            for (int i = 0; i < array.length; i++) {
//                //tmepArray是一个临时数组,用于就是Ri
//                //eg:1,2,3的全排列,先取出1,那么这时tempArray中就是2,3
//                int[] tempArray = new int[array.length-1];
//                System.arraycopy(array,0,tempArray,0,i);
//                System.arraycopy(array,i+1,tempArray,i,array.length-i-1);
//                stack.push(array[i]);
//                perm(tempArray,stack);
//                stack.pop();
//            }
//        }
//    }
//============================================================================
//    private static int cnt = 0;
//    public static void main(String[] args) {
//        perm(new int[]{1,2,3,4},0,3);
//        System.out.println(cnt);
//    }
//
//    /**
//     * @param array
//     * @param start
//     * @param end  这个end不是目标层级,是数组内元素交换的阈值,一直交换到数据末端
//     */
//    public static void perm(int[] array,int start,int end) {
//        if(start==end) {
//            System.out.println(Arrays.toString(array));
//            cnt++;
//
//        } else {
//            for (int i = start; i <= end; i++) {
//                //1,2,3的全排列这块相当于将其中一个提了出来,下次递归从start+1开始
//                swap(array,start,i);
//                perm(array,start+1,end);
//                //这块是复原数组,为了保证下次另外的同级递归使用数组不会出错
//                //这块可以通过树来理解,每次回退一步操作,交换回去
//                swap(array,start,i);
//            }
//        }
//    }
//    public static void swap(int[] array,int i,int j) {
//        int temp = array[i];
//        array[i] = array[j];
//        array[j] = temp;
//    }
//=============================================================================
    /**
     * 第一个问题:
     *
     *   首先,先让我们来看第一个问题, 有1,2,3,4这4个数字.可以重复的在里面选4次,问能得到多少种结果.
      */
//
//static int cnt = 0;
//public static Stack<Integer> stack = new Stack<Integer>();
//    public static void main(String[] args) {
//        int shu[] = {1,2,3};
//        f(shu,3,0);//这里是4不是3,如果3,输出[1, 1, 1]
//        System.out.println(cnt);
//    }
//    /**
//     *
//     * @param shu   待选择的数组
//     * @param targ  目标组合层级, 这里给定之后,值就是是固定的,给定层级为3,那么输出三个数字
//     * @param cur   当前选择的是第几级
//     */
//    private static void f(int[] shu, int targ, int cur) {
//        if(cur == targ) {
//            System.out.println(stack);
//            cnt++;//统计总条数
//            return;
//        }
//
//        for(int i=0;i<shu.length;i++) {
//            stack.add(shu[i]);
//            f(shu, targ, cur+1);//cur就是用来控制树的层级,深度
//            /**
//             *           1           2         3        cur=0
//             *        [1 2 3]     [1 2 3]   [1 2 3]     cur=1
//             *    [123][123][123]      ......           cur=2
//             *    当cur=3时,等于tag,那么开始打印stack,然后开始回溯到cur=2级,index加1,直到shu大小,之后回溯到cur=1级别,继续指index加1.....
//             */
//            stack.pop();//栈用来回溯
//
//        }
//    }
//
//==================================================================================
//    static int cnt = 0;
//    static Stack<Integer> s = new Stack<Integer>();
//    static boolean[] used = new boolean[10000];
//
//    /**
//     * 递归方法,当实际选取的小球数目与要求选取的小球数目相同时,跳出递归
//     * @param minv - 小球编号的最小值
//     * @param maxv - 小球编号的最大值
//     * @param curnum - 当前已经确定的小球的个数
//     * @param maxnum - 要选取的小球的数目
//     */
//    public static void kase2(int minv,int maxv,int curnum, int maxnum){
//        if(curnum == maxnum){
//            cnt++;
//            System.out.println(s);
//            return;
//        }
//
//        for(int i = minv; i <= maxv; i++){
//            if(!used[i]){
//                s.push(i);
//                used[i] = true;
//                kase2(minv, maxv, curnum+1, maxnum);
//                s.pop();
//                used[i] = false;
//            }
//        }
//    }
//
//    public static void main(String[] args){
//        int shu[] = {1,2,3,4};
//        //kase2(shu[], 0, 3);
//        System.out.println(cnt);
//    }
//========================================================

}
原文地址:https://www.cnblogs.com/brxHqs/p/13684608.html