列表的可能组合

发布于 2024-12-01 12:42:25 字数 339 浏览 4 评论 0原文

我有一个对象数组列表,我想创建所有可能的组合(根据一组简单的规则)。存储在列表中的每个对象都包含一个小队编号和一个字符串。这是我存储的典型列表的示例:

0: 1, A
1: 1, B
2: 2, A
3: 2, B
4: 3, C
5: 3, D
6: 4, C
7: 4, D

我想要获取每个小队编号只能出现一次的所有组合,例如: (1,A),(2,A),(3,C),( 4,C) 那么下一个组合将是 (1,A),(2,A),(3,C),(4,D)。 我将如何在java中解决这个问题?通常我会使用嵌套循环,但事实上它全部存储在一个列表中,这让我的事情变得复杂。

谢谢, 脱漆剂

I have an arraylist of objects that I want to create all possible combinations (according to a simple set of rules). Each object that is stored in the list holds a squadNumber and a string. Here is an example of a typical list I am storing:

0: 1, A
1: 1, B
2: 2, A
3: 2, B
4: 3, C
5: 3, D
6: 4, C
7: 4, D

I want to get all the combinations where each squadNumber can only be present once, for example: (1,A),(2,A),(3,C),(4,C) then the next combination would be (1,A),(2,A),(3,C),(4,D).
How would I go about this in java? Usually I would use a nested loop, but the fact that it's all being stored in one list complicates things for me.

Thanks,
paintstripper

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸩远一方 2024-12-08 12:42:25

编辑

算法如下:

  1. 按号码分割所有小队。所以我们有 1 人的小队名单,
    另一个小队 2 的列表,等等。
  2. 运行 dfs。在第 n 步,我们添加第 n 个号码的小队。

代码

// Split squads by numbers, so we can iterate through each number independently.
private Map<Integer, List<Squad>> splitSquadsByNumbers(List<Squad> squads) {
    Map<Integer, List<Squad>> res = new HashMap<Integer, List<Squad>>();
    for (Squad squad : squads) {
        if (res.get(squad.getNumber()) == null) {
            res.put(squad.getNumber(), new ArrayList<Squad>());
        }
        res.get(squad.getNumber()).add(squad);
    }
    return res;
}

List<Integer> squadNumbers;
Map<Integer, List<Squad>> squadsByNumbers;
Stack<Squad> stack;

// Iterating through each squad with number squadNumbers[position] and try to add to stack, at the end pop it from stack.

private void dfs(int position) {
    if (position == squadNumber.size()) {
        System.out.println(stack.toString());
    } else {
        for (Squad squad : squadsByNumbers.get(squadNumber.get(position))) {
            stack.push(squad);
            dfs(position + 1);
            stack.pop();
        }
    }
}

private void main(List<Squad> squads) {
    squadsByNumbers = splitSquadsByNumbers(squads);
    squadNumber = new ArrayList(squadsByNumber.keySet());
    Collections.sort(squadNumbers);
    stack = new Stack<Squad>();
    dfs(0);
}

EDITED

Algorithm is following:

  1. Split all squads by numbers. So we have list with squads for 1,
    another list for squads 2, etc.
  2. Run dfs. At nth step we add squads with nth number.

Code

// Split squads by numbers, so we can iterate through each number independently.
private Map<Integer, List<Squad>> splitSquadsByNumbers(List<Squad> squads) {
    Map<Integer, List<Squad>> res = new HashMap<Integer, List<Squad>>();
    for (Squad squad : squads) {
        if (res.get(squad.getNumber()) == null) {
            res.put(squad.getNumber(), new ArrayList<Squad>());
        }
        res.get(squad.getNumber()).add(squad);
    }
    return res;
}

List<Integer> squadNumbers;
Map<Integer, List<Squad>> squadsByNumbers;
Stack<Squad> stack;

// Iterating through each squad with number squadNumbers[position] and try to add to stack, at the end pop it from stack.

private void dfs(int position) {
    if (position == squadNumber.size()) {
        System.out.println(stack.toString());
    } else {
        for (Squad squad : squadsByNumbers.get(squadNumber.get(position))) {
            stack.push(squad);
            dfs(position + 1);
            stack.pop();
        }
    }
}

private void main(List<Squad> squads) {
    squadsByNumbers = splitSquadsByNumbers(squads);
    squadNumber = new ArrayList(squadsByNumber.keySet());
    Collections.sort(squadNumbers);
    stack = new Stack<Squad>();
    dfs(0);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文