我正在使用一个我创建的包含一组数字数据的链表。我需要找到一种方法来测试这个列表的每一个可能的两个集合的分割情况,以便于做到这一点,我需要将列表分解为每一个可能的两个集合的组合。顺序不重要,可能会有重复。
For instance, for a list of numbers {1 4 3 1}, the possible splits are
{1} and {4, 3, 1}
{4} and {1, 3, 1}
{3} and {1, 4, 1}
{1} and {1, 4, 3}
{1, 4} and {3, 1}
{1, 3} and {4, 1}
{1, 1} and {4, 3}
一个由4个数字组成的列表并不难,但随着列表变得更大,事情会变得更加复杂,我很难看出其中的规律。有人能帮我找到一个算法吗?
编辑:
抱歉,我没有看到问题。这是我迄今为止尝试过的。我的循环结构是错误的。当我在常规数组上尝试后弄清楚我在做什么时,我将扩展算法以适应我的链表。
public class TwoSubsets
{
public static void main(String[] args)
{
int[] list = {1, 3, 5, 7, 8};
int places = 1;
int[] subsetA = new int[10];
int[] subsetB = new int[10];
for (int i = 0; i < list.length; i++)
{
subsetA[i] = list[i];
for (int current = 0; current < (5 - i ); current++)
{
subsetB[current] = list[places];
places++;
}
System.out.print("subsetA = ");
for (int j = 0; j < subsetA.length; j++)
{
System.out.print(subsetA[j] + " ");
}
System.out.println();
System.out.print("subsetB = ");
for (int k = 0; k < subsetB.length; k++)
{
System.out.print(subsetB[k] + " ");
}
}
}
}