【问题标题】:How to get 2D array possible combinations如何获得二维数组可能的组合
【发布时间】:2013-03-29 22:24:29
【问题描述】:

我有以下二维数组:

String[M][]

String[0]
   "1","2","3"

String[1]
   "A", "B"
   .
   .
   .
String[M-1]
   "!"

所有可能的组合都应存储在结果数组中 String[] combinations。比如:

combinations[0] == {"1A....!")
combinations[1] == {"2A....!") 
combinations[2] == {"3A....!") 
combinations[3] == {"1B....!")

请注意,数组的长度是可变的。输出字符串中元素的顺序无关紧要。我也不在乎是否有重复。

如果数组长度相同,嵌套循环可以解决问题,但事实并非如此,我真的不知道如何解决这个问题。

【问题讨论】:

    标签: java combinations multidimensional-array


    【解决方案1】:

    您可以像发条一样一次一个地遍历组合,方法是使用一个数组来记录每个内部数组的大小,并使用一个计数器数组来跟踪每个内部数组中要使用的成员。类似这种方法:

    /**
     * Produce a List<String> which contains every combination which can be
     * made by taking one String from each inner String array within the
     * provided two-dimensional String array.
     * @param twoDimStringArray a two-dimensional String array which contains
     * String arrays of variable length.
     * @return a List which contains every String which can be formed by taking
     * one String from each String array within the specified two-dimensional
     * array.
     */
    public static List<String> combinations(String[][] twoDimStringArray) {
        // keep track of the size of each inner String array
        int sizeArray[] = new int[twoDimStringArray.length];
    
        // keep track of the index of each inner String array which will be used
        // to make the next combination
        int counterArray[] = new int[twoDimStringArray.length];
    
        // Discover the size of each inner array and populate sizeArray.
        // Also calculate the total number of combinations possible using the
        // inner String array sizes.
        int totalCombinationCount = 1;
        for(int i = 0; i < twoDimStringArray.length; ++i) {
            sizeArray[i] = twoDimStringArray[i].length;
            totalCombinationCount *= twoDimStringArray[i].length;
        }
    
        // Store the combinations in a List of String objects
        List<String> combinationList = new ArrayList<String>(totalCombinationCount);
    
        StringBuilder sb;  // more efficient than String for concatenation
    
        for (int countdown = totalCombinationCount; countdown > 0; --countdown) {
            // Run through the inner arrays, grabbing the member from the index
            // specified by the counterArray for each inner array, and build a
            // combination string.
            sb = new StringBuilder();
            for(int i = 0; i < twoDimStringArray.length; ++i) {
                sb.append(twoDimStringArray[i][counterArray[i]]);
            }
            combinationList.add(sb.toString());  // add new combination to list
    
            // Now we need to increment the counterArray so that the next
            // combination is taken on the next iteration of this loop.
            for(int incIndex = twoDimStringArray.length - 1; incIndex >= 0; --incIndex) {
                if(counterArray[incIndex] + 1 < sizeArray[incIndex]) {
                    ++counterArray[incIndex];
                    // None of the indices of higher significance need to be
                    // incremented, so jump out of this for loop at this point.
                    break;
                }
                // The index at this position is at its max value, so zero it
                // and continue this loop to increment the index which is more
                // significant than this one.
                counterArray[incIndex] = 0;
            }
        }
        return combinationList;
    }
    

    方法的工作原理

    如果您将计数器数组想象成一个数字时钟读数,那么第一个字符串组合会看到计数器数组全为零,因此第一个字符串是由每个内部数组的零元素(第一个成员)构成的。

    为了获得下一个组合,计数器数组加一。因此,最不重要的计数器索引增加一。如果这导致它的值变得等于它所代表的内部数组的长度,则索引为零,并增加下一个更重要的索引。一个单独的大小数组存储每个内部数组的长度,以便计数器数组循环知道索引何时达到其最大值。

    例如,如果大小数组是:

    [3][3][2][1]
    

    计数器数组位于:

    [0][2][1][0]
    

    那么增量将使最不重要(最右边)的索引等于 1,这是它的最大值。因此该索引归零,下一个更重要的索引(从右数第二个)增加到 2。但这也是该索引的最大值,所以它被归零,我们移动到下一个更重要的索引。它增加到三,这是它的最大值,所以它被归零,我们移动到最重要的(最左边的)索引。该值增加到 1,小于其最大值,因此递增的计数器数组变为:

    [1][0][0][0]
    

    这意味着下一个字符串组合是通过取第一个内部数组的第二个成员和接下来三个内部数组的第一个成员来组成的。

    可怕的警告和注意事项

    我刚刚在大约四十分钟内写了这篇文章,现在是早上半点,这意味着即使它看起来完全符合需要,但很可能存在可以优化的错误或代码位。因此,如果它的性能至关重要,请务必对其进行彻底的单元测试。

    请注意,它返回一个 List 而不是 String 数组,因为我认为 Java 集合在大多数情况下比使用数组更可取。此外,如果您需要一个没有重复的结果集,您可以简单地将 List 更改为一个 Set,它将自动删除重复项并为您留下一个唯一的集合。

    如果您确实需要将结果作为 String 数组,别忘了您可以使用 List&lt;String&gt;.toArray(String[]) 方法将返回的 List 简单地转换为您需要的内容。

    【讨论】:

    • 老兄,您的代码有效,非常感谢。但是我一直在看,不知道为什么,您能否对您使用的算法进行一点解释-基本步骤,以及每个步骤的一些细节,我只是不明白它背后的魔力。
    • @lekroif 粗略一看,它似乎计算了您可以进行的组合总数,然后初始化一个计数器数组,每个组合数组一个 - 一端的计数器循环,并且每次循环它都会增加下一个,如果循环,下一个会增加,依此类推。想想带玻璃杯的公文包锁。
    • @lekroif 我在运行该方法的工作原理的代码之后添加了一个大块。 Patashu 是对的:它就像一个数字表盘,每次在一个位置打勾,以产生下一个组合的坐标,直到它遍历所有可能的坐标组合。我还修改了上面的代码以删除我发布的第一个版本中不必要的归零循环。
    【解决方案2】:

    这个问题有一个非常好的递归结构(这也意味着它可能会在内存中爆炸,正确的方法应该是使用迭代器,如另一个答案,但这个解决方案看起来更好 imo,我们可以归纳证明正确性,因为递归性质)。组合由第一个列表中的一个元素组成,该元素附加到由剩余 (n-1) 个列表形成的所有可能组合。递归工作在 AllCombinationsHelper 中完成,但您调用 AllCombinations。注意测试空列表和更广泛。

    public static List<String> AllCombinations(List<List<Character>> aList) {
        if(aList.size() == 0) { return new ArrayList<String>(); }
        List<Character> myFirstSubList = aList.remove(0);
        List<String> myStrings = new ArrayList<String>();
        for(Character c : myFirstSubList) {
            myStrings.add(c.toString());
        }
    
        return AllCombinationsHelper(aList, myStrings);
    }
    
    public static List<String> AllCombinationsHelper(List<List<Character>> aList, 
                                                     List<String> aCollection) {
        if(aList.size() == 0) { return aCollection; }
        List<Character> myFirstList = aList.remove(0);
        List<String> myReturnSet = new ArrayList<String>();
    
        for(String s : aCollection) {
            for(Character c : myFirstList) {
                myReturnSet.add(c + s);
            }
        }
    
        return AllCombinationsHelper(aList, myReturnSet);
    }
    

    【讨论】:

      【解决方案3】:

      应该直接处理递归。

      让我重新措辞一下,这样术语就不那么混乱了。

      我们将 String[] 称为 Token List,这是一个 Token 列表

      现在您有一个 Token List 列表,您想从每个可用的 Token List 中获取一个 Token,并找出所有组合。

      你需要做的是,给定一个 TokenList 列表

      • 如果List只有一个TokenList,则Token List本身的内容就是所有的组合
      • 否则,通过排除第一个令牌列表来制作子列表,并找出该子列表的所有组合。当您拥有组合时,答案就是简单地遍历您的第一个标记列表,并使用标记列表中的每个标记生成所有组合,以及结果组合。

      我只是给出一个伪代码:

      List<String> allCombinations(List<TokenList> listOfTokenList) {
        if (length of strings == 1) {
          return strings[0];
        }
      
      
        List<String> subListCombinations 
            = allCombination(listOfTokenList.subList(1));  // sublist from index 1 to the end
      
      
        List<String> result;
        for each (token in listOfTokenList[0]) {
          for each (s in subListCombination) {
            result.add(token + s);
          }
        }
        return result;
      }
      

      【讨论】:

        【解决方案4】:

        我已经为这个问题苦苦挣扎了一段时间。但我终于解决了。我的主要障碍是我用于声明每个变量的范围。如果您没有在正确的范围内声明您的变量,那么该变量将保留在之前的迭代中所做的更改。

        import java.util.ArrayList;
        import java.util.Arrays;
        import java.util.List;
        
        public class RecursiveAlgorithmTest {
            private static int recursiveCallsCounter = 0;
            public static ArrayList<ArrayList<String>> testCases = new ArrayList<ArrayList<String>>();
        
            /**
             * @param args the command line arguments
             */
            public static void main(String[] args) {
                //set values for ArrayOfArrays
                ArrayList<String> VariableA = new ArrayList<String>(Arrays.asList("red", "green"));
                ArrayList<String> VariableB = new ArrayList<String>(Arrays.asList("A", "B", "C"));
                ArrayList<String> VariableC = new ArrayList<String>(Arrays.asList("1", "2", "3", "4"));
        
                ArrayList<ArrayList<String>> AofA = new ArrayList<ArrayList<String>>();
                AofA.add(VariableA); AofA.add(VariableB); AofA.add(VariableC);
        
                System.out.println("Array of Arrays: ToString(): " +AofA.toString());
        
                ArrayList<String> optionsList = new ArrayList<String>();
        
                //recursive call
                recurse(optionsList, AofA, 0);
        
                for (int i = 0 ; i < testCases.size() ; i++) {
                    System.out.println("Test Case " + (i+1) + ": " + testCases.get(i));
                    }
        
                }//end main(String args[])
        
        
        
            private static void recurse(ArrayList<String> newOptionsList, 
                ArrayList<ArrayList<String>> newAofA, int placeHolder){
                recursiveCallsCounter++;
                System.out.println("\n\tStart of Recursive Call: " + recursiveCallsCounter);
                System.out.println("\tOptionsList: " + newOptionsList.toString());
                System.out.println("\tAofA: " + newAofA.toString());
                System.out.println("\tPlaceHolder: "+ placeHolder);
        
                //check to see if we are at the end of all TestAspects
                if(placeHolder < newAofA.size()){
        
                    //remove the first item in the ArrayOfArrays
                    ArrayList<String> currentAspectsOptions = newAofA.get(placeHolder);
                    //iterate through the popped off options
        
        
        
        
                    for (int i=0 ; i<currentAspectsOptions.size();i++){
                        ArrayList<String> newOptions = new ArrayList<String>();
                        //add all the passed in options to the new object to pass on
                        for (int j=0 ; j < newOptionsList.size();j++) {
                            newOptions.add(newOptionsList.get(j));
                        }
        
                        newOptions.add(currentAspectsOptions.get(i));
                        int newPlaceHolder = placeHolder + 1;
                        recurse(newOptions,newAofA, newPlaceHolder);
                    }
                } else { // no more arrays to pop off
                    ArrayList<String> newTestCase = new ArrayList<String>();
                    for (int i=0; i < newOptionsList.size();i++){
                        newTestCase.add(newOptionsList.get(i));
                    }
                    System.out.println("\t### Adding: "+newTestCase.toString());
                    testCases.add(newTestCase);
                }
            }//end recursive helper 
        }// end of test class
        

        【讨论】:

          【解决方案5】:

          在 Python 中使用 itertools.product 和参数解包(应用)

          >>> import itertools
          >>> S=[['1','2','3'],['A','B'],['!']]
          >>> ["".join(x) for x in itertools.product(*S)]
          ['1A!', '1B!', '2A!', '2B!', '3A!', '3B!']
          

          【讨论】:

            猜你喜欢
            • 2013-02-12
            • 2023-03-20
            • 2012-06-05
            • 1970-01-01
            • 2019-04-18
            • 2012-07-04
            • 2019-10-25
            • 2011-05-12
            • 1970-01-01
            相关资源
            最近更新 更多