Java:将列表拆分为随机长度的n部分
我正试图把一份清单分成几个部分。我需要找到所有可能的方法,n个整数可以分布在k个群上。例如,如果我的列表是{1,2,3,4,5,6,7},我需要找到将其拆分为k=3组的所有方法。我想强调的是,我需要知道所有可能的分割方式,这对于n和/或k的大值来说是一个巨大的数目。然而,我希望这些数字保持相对较小(个位数),因此应该没有问题。我有一段代码将列表一分为二:Java:将列表拆分为随机长度的n部分,java,algorithm,list,arraylist,split,Java,Algorithm,List,Arraylist,Split,我正试图把一份清单分成几个部分。我需要找到所有可能的方法,n个整数可以分布在k个群上。例如,如果我的列表是{1,2,3,4,5,6,7},我需要找到将其拆分为k=3组的所有方法。我想强调的是,我需要知道所有可能的分割方式,这对于n和/或k的大值来说是一个巨大的数目。然而,我希望这些数字保持相对较小(个位数),因此应该没有问题。我有一段代码将列表一分为二: List<List<Integer>> results; public void findAllSu
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);
}
}
}
列出结果;
public void findallpublists(列表列表,int minimalthengthofspublist){
结果=新的ArrayList();
列表子列表=新建ArrayList();
for(int i=0;i=minimalengthofsublist){
结果:添加(子列表);
}
}
}
私有静态void recursiveSublist(列表列表、列表子列表、int sublistSize、列表currentSubList、int startIndex){
如果(子列表大小==0){
子列表。添加(当前子列表);
}否则{
次地峡大小--;
对于(inti=startIndex;i
我意识到您可能会执行另一个级别的递归,将子列表拆分为更小的子列表,直到达到所需数量的子列表。但是我没有看到最有效的方法,递归不是我的强项,所以我希望有人能给我指出正确的方向。非常感谢您的帮助。谢谢。如果我的理解是正确的,这就是您要找的:
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);
}
}
}
导入java.util.array;
公共类组合{
公共静态void main(字符串[]args){
字符串[]arr={“A”、“B”、“C”、“D”、“E”、“F”};
组合2(arr,3,0,新字符串[3]);
}
静态无效组合2(字符串[]arr、int len、int startPosition、字符串[]result){
如果(len==0){
System.out.println(Arrays.toString(result));
返回;
}
对于(int i=startPosition;i来说,这里有一种方法:
@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);
}
}
}
我相信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}
.4将输入分成两组的组合。如果这是他想要的,我完全误解了。Sorry@Adi我意识到我的问题有点含糊不清。对不起,谢谢你的贡献。为了确保我正确理解你的意思:你想要输入{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}
,将输入分为两组的4个组合。因为每个组(例如{1,2}
)是一个列表
,则每个组合都是一个列表
,因此全套组合必须是一个列表
,而您的代码没有这样的列表,因此无法提供所需的结果。是的,这是正确的。如果我的问题不清楚,我很抱歉。看起来就是这样。非常感谢您的帮助。干杯。
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]