全排列

package com.tianjunwei.rpc;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 全排列
 */
public class A {
    
    public static void main(String[] args) {

        Integer a[] = {1, 2, 3, 4 ,5};

        List<Integer> b = new ArrayList<>();

        List c = new ArrayList<>(Arrays.asList(a));

        print(c , b );

    }

    public static void print(List<Integer> a ,List<Integer> b) {

        if(b.size() == a.size()) {
            for(int k = 0; k < b.size() ; k++) {
                System.out.print(b.get(k) + " ");
            }
            System.out.println();
            return ;
        }

        int len = a.size();

        for(int i = 0 ; i < len  ; i++) {

           Integer ele =  a.get(i);

           if(b.contains(ele)) {
                continue;
           }

           b.add(ele);
           
           print(a , b);

           b.remove(b.size() - 1);
        }
    }
}

  

posted @ 2019-03-17 20:50  han_shan_chang  阅读(126)  评论(0编辑  收藏  举报