zoukankan      html  css  js  c++  java
  • 排列组合问题

    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);
    //    }
    //========================================================
    
    }
    
  • 相关阅读:
    Golang Failpoint 的设计与实现
    没涉及到最值求解;观点:矩阵乘法无法表达出结果。 现实生活中事件、现象的数学表达
    多元微分学 枚举破解15位路由器密码 存储空间限制 拆分减长,求最值 数据去重
    ARP Poisoning Attack and Mitigation Techniques ARP欺骗 中间人攻击 Man-In-The-Middle (MITM) attack 嗅探 防范 Can one MAC address have two different IP addresses within the network?
    The C10K problem
    HTTP Streaming Architecture HLS 直播点播 HTTP流架构
    现代IM系统中消息推送和存储架构的实现
    现代IM系统中的消息系统架构
    长连接锁服务优化实践 C10K问题 nodejs的内部构造 limits.conf文件修改 sysctl.conf文件修改
    doubleclick cookie、动态脚本、用户画像、用户行为分析和海量数据存取 推荐词 京东 电商 信息上传 黑洞 https://blackhole.m.jd.com/getinfo
  • 原文地址:https://www.cnblogs.com/brxHqs/p/13684608.html
Copyright © 2011-2022 走看看