• 数组元素全排列Java


    import java.util.*;
    
    /**
     * 数组全排序
     * @author Green.Gee
     * @date 2022/11/18 20:33
     * @email green.gee.lu@gmail.com
     */
    public class FullMutation {
    
        // 数组元素全排列
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            // 示例 1 2 3
            while (in.hasNextInt()) { // 注意 while 处理多个 case
                int a = in.nextInt();
                int b = in.nextInt();
                int c = in.nextInt();
                int[] arr = new int[]{a,b,c};
    
                // 递归方法
    //            mutation(arr,0,arr.length - 1);
    
                // 插入组合方法
                mutation(arr);
            }
        }
    
        static void swap(int [] arr,int i,int j){
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    
    
    
        public static void mutation(int [] arr){
            List<List<Integer>>  res = new LinkedList<>();
            res.add(0,Arrays.asList(arr[0]));
    
            for(int i = 1; i < arr.length; i++){
    
                List<List<Integer>>  temp_res = new LinkedList<>();
                List<Integer> next = Arrays.asList(arr[i]);
                for(List<Integer> item : res){
                    List<Integer> p = new LinkedList<>();
                    p.addAll(next);
                    p.addAll(item);
                    temp_res.add(p);
    
                    List<Integer> n = new LinkedList<>();
                    n.addAll(item);
                    n.addAll(next);
                    temp_res.add(n);
    
                    for(int j = 1; j < item.size(); j++){
    
                        List<Integer> m = new LinkedList<>();
                        m.addAll(item.subList(0,j));
                        m.addAll(next);
                        m.addAll(item.subList(j,item.size()));
                        temp_res.add(m);
    
    
                    }
                }
                res = temp_res;
            }
    
            System.err.println(res.toString());
    
        }
    
        public static void mutation(int [] arr,int start,int end){
            if(start == end){
                System.out.println(Arrays.toString(arr));
                return;
            }
    
            for(int i = start;i <= end;i++){
                swap(arr,start,i);
                mutation(arr,start + 1,end);
                swap(arr,start,i);
            }
    
        }
        
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
  • 相关阅读:
    手写maxpooling
    Win11怎么安装语音包?Win11语音包安装教程
    android dex 优化
    左耳听风-何为技术领导力
    zephyr线程生命周期
    Element UI +Vue页面生成二维码的方法
    OWASP TOP 10解析:构建坚不可摧的Web应用安全防线
    Java 字符流案例_集合与文件内容的相互转化(升级版)
    想要顺利拿下订单,外贸人的价格谈判思维你GET了吗?
    六月集训(29)分治
  • 原文地址:https://blog.csdn.net/weixin_38233104/article/details/127955143