排列组合

2017-11-01  本文已影响0人  哲哲哥
import java.util.LinkedList;
import java.util.List;
public class Solution {
    public static List<List<Integer>> ans=new LinkedList<List<Integer>>();
    public static boolean[] v=new boolean[100];
    public static LinkedList<Integer> list=new LinkedList<Integer>();
    public static void robot(int index,int[] nums){
        if (index>=nums.length) {
            System.out.println(list);
            List<Integer> list2=new LinkedList<Integer>();
            for (Integer i : list) {
                list2.add(i);
            }
            ans.add(list2);
            return;
        }
        for(int i=0;i<nums.length;i++){
            if (v[i]==false) {
                v[i]=true;
                list.add(nums[i]);
                robot(index+1, nums);
                list.pollLast();
                v[i]=false;
            }
        }
    }
    
    public static void main(String[] args) {
        int []arr={1,2,3};
        robot(0, arr);
        System.out.println(ans);
    }

}
import java.util.LinkedList;
import java.util.List;

public class Solution2 {
    public static List<List<Integer>> ans = new LinkedList<List<Integer>>();
    public static LinkedList<Integer> list = new LinkedList<Integer>();

    public static void robot(int index, int[] nums,int k) {
        if (k==0) {
            System.out.println(list);
            List<Integer> list2 = new LinkedList<Integer>();
            for (Integer i : list) {
                list2.add(i);
            }
            ans.add(list2);
            return;
        }
        for (int i = index+1; i < nums.length; i++) {
            list.add(nums[i]);
            robot(i, nums, k-1);
            list.pollLast();
        }
    }

    public static void main(String[] args) {
        int []arr={1,2,3,0};
        robot(-1, arr,2);
        System.out.println(ans);
    }

}

上一篇下一篇

猜你喜欢

热点阅读