java - 查找给定数组大小中给定数字的所有组合并存储到矩阵中

时间:2017-11-16 08:44:28

标签: java

我正在寻找一种在public static void main(String [] args)中创建matrix [] []的方法,矩阵[] []的目的 在static void combinationUtil()中存储数据[],原始类排列输出如

1 2 3 4

1 2 3 5

1 2 4 5

1 3 4 5

2 3 4 5

我需要创建的矩阵[] []输出和类排列一样,就像

class permutation {

    static void combinationUtil(int arr[], int data[], int start, int end,int index, int r)
    {
        if (index == r)
        {
            for (int j=0; j<r; j++)
            {
                System.out.print(data[j]+" ");
            }
            System.out.println("");
            return;
        }

        for (int i=start; i<=end && end-i+1 >= r-index; i++)
        {
            data[index] = arr[i];
            combinationUtil(arr, data, i+1, end, index+1, r);
        }
    }

    public static void main (String[] args) {
        int arr[] = {1, 2, 3, 4, 5};
        int r = 4;
        int n = arr.length;
        int data[]=new int[r];
        int start=0,end=n-1,index=0;
        combinationUtil(arr, data, start, end, index, r);
    }
}

原班:

<input type="image" src="http://somesite.com/somepath/someimage.png" />

1 个答案:

答案 0 :(得分:0)

使用此代码,您可以找到任何已知输入数组和序列长度的可能组合。它不是那么基本的理解,但就我所知,它是唯一真正的非递归解决方案。希望这可以帮助。 :)

public static void main(String[] args) {                                                              


     int[] input = {1, 2, 3, 4, 5};    // input array                                                 
     int k = 4;                             // sequence length                                        

     List<int[]> subsets = new ArrayList<>();                                                         

     int[] s = new int[k];                  // here we'll keep indices                                
                                            // pointing to elements in input array                    

     if (k <= input.length) {                                                                         
         // first index sequence: 0, 1, 2, ...                                                        
         for (int i = 0; (s[i] = i) < k - 1; i++);                                                    
         subsets.add(getSubset(input, s));                                                            
         for(;;) {                                                                                    
             int i;                                                                                   
             // find position of item that can be incremented                                         
             for (i = k - 1; i >= 0 && s[i] == input.length - k + i; i--);                            
             if (i < 0) {                                                                             
                 break;                                                                               
             }                                                                                        
             s[i]++;                    // increment this item                                        
             for (++i; i < k; i++) {    // fill up remaining items                                    
                 s[i] = s[i - 1] + 1;                                                                 
             }                                                                                        
             subsets.add(getSubset(input, s));                                                        
         }                                                                                            
     }                                                                                                
        System.out.println(Arrays.deepToString(subsets.toArray()));                                   

}                                                                                                     

    // generate actual subset by index sequence                                                       
   static int[] getSubset(int[] input, int[] subset) {                                                
        int[] result = new int[subset.length];                                                        
        for (int i = 0; i < subset.length; i++)                                                       
            result[i] = input[subset[i]];                                                             
        return result;                                                                                
    }                                                                                                 


  } 

  Outputs:
  [1, 2, 3, 4]
  [1, 2, 3, 5]
  [1, 2, 4, 5]
  [1, 3, 4, 5]
  [2, 3, 4, 5]                                                                                                        
相关问题