【问题标题】:Java: split list into n parts of random lengthJava:将列表拆分为随机长度的n部分
【发布时间】:2015-12-05 16:02:39
【问题描述】:

我正在尝试将列表拆分为几个部分。我需要找到 n 个整数可以分布在 k 个组上的所有可能方式。例如,如果我的列表是 {1,2,3,4,5,6,7},我需要找到可以将其分成 k=3 组的所有方法。我想强调的是,我需要知道所有可能的拆分方式,这对于较大的 n 和/或 k 值来说是一个巨大的数量。但是,我希望这些保持相对较小(个位数),所以应该没有问题。我有这段代码将列表分成两部分:

List<List<Integer>> results;        

public void findAllSublists(List<Integer> list, int minimalLengthOfSublist) {
    results = new ArrayList<List<Integer>>();
    List<List<Integer>> sublists = new ArrayList<List<Integer>>();
    for (int i = 0; i <= list.size(); i++) {
        recursiveSublist(list, sublists, i, new ArrayList<Integer>(), 0);
    }
    for (List<Integer> subList : sublists) {
        if (subList.size() >= minimalLengthOfSublist) {
            results.add(subList);
        }
    }       
}
private static void recursiveSublist(List<Integer> list, List<List<Integer>> subLists, int sublistSize, List<Integer> currentSubList, int startIndex) {
    if (sublistSize == 0) {
        subLists.add(currentSubList);
    } else {
        sublistSize--;
        for (int i = startIndex; i < list.size(); i++) {
            List<Integer> newSubList = new ArrayList<Integer>(currentSubList);
            newSubList.add(list.get(i));
            recursiveSublist(list, subLists, sublistSize, newSubList, i + 1);
        }
    }
}

我意识到您可能会进行另一个级别的递归,将子列表拆分为更小的子列表,直到达到所需数量的子列表。但是我没有看到最有效的方法,递归不是我的强项,所以我希望有人能指出我正确的方向。帮助将不胜感激。谢谢。

【问题讨论】:

  • 只是为了确保我理解正确:您希望输入 {1,2,3,4,5}k=2 以返回:{1},{2,3,4,5}{1,2},{3,4,5}{1,2,3},{4,5}{1,2,3,4},{5}。将输入分成 2 组的 4 种组合。由于每个组(例如{1,2})是一个List,那么每个组合都是一个List&lt;List&gt;,所以全套组合必须是一个List&lt;List&lt;List&gt;&gt;,而你的代码没有这样的列表,所以它无法提供请求的结果。
  • 是的,没错。如果我的问题不清楚,我深表歉意。

标签: java algorithm list arraylist split


【解决方案1】:

这是一种方法:

@SuppressWarnings("unchecked")
private static List<List<List<Integer>>> split(List<Integer> list, int groups) {
    if (groups <= 0 || groups > list.size())
        throw new IllegalArgumentException("Invalid number of groups: " + groups +
                                           " (list size: " + list.size() + ")");
    List<List<List<Integer>>> result = new ArrayList<>();
    split(list, 0, new List[groups], 0, result);
    return result;
}
private static void split(List<Integer> list, int listIdx,
                          List<Integer>[] combo, int comboIdx,
                          List<List<List<Integer>>> result) {
    if (combo.length - comboIdx == 1) {
        combo[comboIdx] = list.subList(listIdx, list.size());
        result.add(new ArrayList<>(Arrays.asList(combo)));
    } else {
        for (int i = 0; i <= (list.size() - listIdx) - (combo.length - comboIdx); i++) {
            combo[comboIdx] = list.subList(listIdx, listIdx + 1 + i);
            split(list, listIdx + 1 + i, combo, comboIdx + 1, result);
        }
    }
}

测试

public static void main(String[] args) {
    test(Arrays.asList(1,2,3,4,5), 2);
    test(Arrays.asList(1,2,3,4,5,6,7), 3);
}
private static void test(List<Integer> list, int groups) {
    System.out.println("Split of " + list + " into " + groups + " groups:");
    for (List<List<Integer>> combo : split(list, groups)) {
        String sep = "  ";
        for (List<Integer> group : combo) {
            System.out.print(sep + group);
            sep = ", ";
        }
        System.out.println();
    }
}

输出

Split of [1, 2, 3, 4, 5] into 2 groups:
  [1], [2, 3, 4, 5]
  [1, 2], [3, 4, 5]
  [1, 2, 3], [4, 5]
  [1, 2, 3, 4], [5]
Split of [1, 2, 3, 4, 5, 6, 7] into 3 groups:
  [1], [2], [3, 4, 5, 6, 7]
  [1], [2, 3], [4, 5, 6, 7]
  [1], [2, 3, 4], [5, 6, 7]
  [1], [2, 3, 4, 5], [6, 7]
  [1], [2, 3, 4, 5, 6], [7]
  [1, 2], [3], [4, 5, 6, 7]
  [1, 2], [3, 4], [5, 6, 7]
  [1, 2], [3, 4, 5], [6, 7]
  [1, 2], [3, 4, 5, 6], [7]
  [1, 2, 3], [4], [5, 6, 7]
  [1, 2, 3], [4, 5], [6, 7]
  [1, 2, 3], [4, 5, 6], [7]
  [1, 2, 3, 4], [5], [6, 7]
  [1, 2, 3, 4], [5, 6], [7]
  [1, 2, 3, 4, 5], [6], [7]

【讨论】:

  • 看起来就是这样。非常感谢您的帮助。干杯。
【解决方案2】:

如果我的理解是正确的,这就是你要找的:

import java.util.Arrays;

public class Combination {
    public static void main(String[] args){
        String[] arr = {"A","B","C","D","E","F"};
        combinations2(arr, 3, 0, new String[3]);
    }

    static void combinations2(String[] arr, int len, int startPosition, String[] result){
        if (len == 0){
            System.out.println(Arrays.toString(result));
            return;
        }       
        for (int i = startPosition; i <= arr.length-len; i++){
            result[result.length - len] = arr[i];
            combinations2(arr, len-1, i+1, result);
        }
    }       
}

在这个线程中找到它: Algorithm to return all combinations of k elements from n

希望对你有帮助。

【讨论】:

  • 我相信 OP 希望输入 {1,2,3,4,5}k=2 以返回:{1},{2,3,4,5}{1,2},{3,4,5}{1,2,3},{4,5}{1,2,3,4},{5}。将输入分成 2 组的 4 种组合。
  • 如果这是他想要的,我完全误解了。对不起
  • @Adi 我意识到我的问题有点含糊不清。抱歉,感谢您的贡献。
猜你喜欢
  • 2011-01-08
  • 1970-01-01
  • 1970-01-01
  • 2015-11-15
  • 1970-01-01
  • 2020-10-01
  • 1970-01-01
  • 2011-03-22
  • 2015-11-19
相关资源
最近更新 更多