全排列解析

Given an array of size n, generate and print all possible combinations of r elements in array. For example, if input array is {1, 2, 3, 4} and r is 2, then output should be {1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 4} and {3, 4}.

 1 import java.util.*;
 2 public class Main{
 3    public static void main(String args[]){
 4        Scanner in =new Scanner(System.in);
 5        while(in.hasNext()){
 6            int n=in.nextInt();
 7            int k=in.nextInt();
 8            printCombination(n, k);
 9        }
10        in.close();
11    }
12 
13     static void combinationUtil(int arr[], int data[], int start,
14                                 int end, int index, int r)
15     {
16         // Current combination is ready to be printed, print it
17         if (index == r)
18         {
19             for (int j=0; j<r; j++)
20                 System.out.print(data[j]+" ");
21             System.out.println("");
22             return;
23         }
24 
25         // replace index with all possible elements. The condition
26         // "end-i+1 >= r-index" makes sure that including one element
27         // at index will make a combination with remaining elements
28         // at remaining positions
29         for (int i=start; i<=end && end-i+1 >= r-index; i++)
30         {
31             data[index] = arr[i];
32             combinationUtil(arr, data, i+1, end, index+1, r);
33         }
34     }
35 
36     // The main function that prints all combinations of size r
37     // in arr[] of size n. This function mainly uses combinationUtil()
38     static void printCombination(int n, int r)
39     {
40         int arr[]=new int[n];
41         for(int i=0;i<n;i++){
42             arr[i]=i+1;
43         }
44         // A temporary array to store all combination one by one
45         int data[]=new int[r];
46 
47         // Print all combination using temprary array 'data[]'
48         combinationUtil(arr, data, 0, n-1, 0, r);
49     }
50 }

How to handle duplicates?
Note that the above method doesn’t handle duplicates. For example, if input array is {1, 2, 1} and r is 2, then the program prints {1, 2} and {2, 1} as two different combinations. We can avoid duplicates by adding following two additional things to above code.
1) Add code to sort the array before calling combinationUtil() in printCombination()
2) Add following lines at the end of for loop in combinationUtil()

// Since the elements are sorted, all occurrences of an element
        // must be together
        while (arr[i] == arr[i+1])
             i++; 

 

//    private static void printCombination(int[] arr,int n, int index, int k) {
//        if(k==index){
//            for(int i=0;i<index;i++){
//                System.out.print(data.get(i));
//            }
//            System.out.println();
//            return;
//        }
//        for(int i=0;i<n;i++){
//            if(vis[i]==0){
//                data.add(i);
//                vis[i]=1;
//                index++;
//                printCombination(arr,n,index,k);
//                data.remove(data.size()-1);
//                index--;
//                vis[i]=0;
//            }
//        }
//    }

 

posted @ 2016-10-09 16:44  kimi9py  阅读(199)  评论(0编辑  收藏  举报