您的位置:首页 >生成所有将长度为 n 的数组划分为恰好 k 个连续非空子数组的组合,可以通过递归或迭代的方式实现。以下是具体的思路和代码示例:思路解析要将一个长度为 n 的数组
发布于2026-03-16 阅读(0)
扫一扫,手机访问

本文讲解如何系统性地生成一个长度为 n 的数组的所有恰好 k 段连续子数组划分方案,核心思路是枚举 k−1 个合法分割点(即在 n−1 个可选间隙中选 k−1 个),并提供清晰、正确、可复用的 Java 实现。
该问题本质是组合数学中的分段计数问题:给定一个含 n 个连续元素的数组(如 [1,2,3,4,5,6]),要求将其唯一地划分为 k 个非空、相邻、不重叠的子数组(例如 [[1], [2,3], [4,5,6]]),且所有子数组必须保持原始顺序与连续性。
关键直觉在于:
⚠️ 注意:原问题约束“子数组至少 1 元素”已由“在间隙处切割”天然保证;“连续性”由子数组取 subList(start, end) 严格保障。
你提供的 Java 递归实现存在以下关键逻辑错误:
这些问题共同导致输出缺失、重复或格式错乱(如出现 [1,2,3,4,5,6] 单段,或 [1,3] 等非法非连续子数组)。
我们采用两阶段策略:
以下是修复后的完整 Java 实现(含详细注释):
import java.util.*;
public class ArrayPartitionGenerator {
/**
* 生成长度为 n 的自然数数组 [1,2,...,n] 的所有恰好 k 段连续子数组划分
* @param n 数组长度
* @param k 子数组段数
* @return 三维列表:List<List<List<Integer>>>
*/
public static List<List<List<Integer>>> generatePartitions(int n, int k) {
if (k <= 0 || k > n) return Collections.emptyList();
// 构造源数组 [1, 2, ..., n]
List<Integer> arr = new ArrayList<>();
for (int i = 1; i <= n; i++) arr.add(i);
// 所有可能的分割位置:在索引 1~n-1 后切(即切在第 i 个元素之后)
List<Integer> splitCandidates = new ArrayList<>();
for (int i = 1; i < n; i++) splitCandidates.add(i);
List<List<List<Integer>>> result = new ArrayList<>();
// 生成所有 C(n-1, k-1) 种分割点组合
List<List<Integer>> allSplitCombinations = generateCombinations(splitCandidates, k - 1);
for (List<Integer> splits : allSplitCombinations) {
List<List<Integer>> partition = new ArrayList<>();
int start = 0;
// 按升序分割点依次切分
for (int end : splits) {
partition.add(new ArrayList<>(arr.subList(start, end)));
start = end;
}
// 添加最后一段
partition.add(new ArrayList<>(arr.subList(start, n)));
result.add(partition);
}
return result;
}
// 辅助方法:生成 list 中所有 size 元素的组合(无重复、有序)
private static List<List<Integer>> generateCombinations(List<Integer> list, int size) {
List<List<Integer>> result = new ArrayList<>();
combine(list, size, 0, new ArrayList<>(), result);
return result;
}
private static void combine(List<Integer> list, int size, int start,
List<Integer> current, List<List<Integer>> result) {
if (current.size() == size) {
result.add(new ArrayList<>(current));
return;
}
for (int i = start; i <= list.size() - (size - current.size()); i++) {
current.add(list.get(i));
combine(list, size, i + 1, current, result);
current.remove(current.size() - 1);
}
}
// 示例主函数
public static void main(String[] args) {
int n = 6, k = 3;
List<List<List<Integer>>> partitions = generatePartitions(n, k);
System.out.println("Total partitions: " + partitions.size());
for (int i = 0; i < partitions.size(); i++) {
System.out.printf("Combination %d: %s%n", i + 1, partitions.get(i));
}
}
}运行上述代码,输出 10 种划分,顺序与题目示例一致(注意:组合生成顺序依赖 generateCombinations 的字典序,可通过 Collections.sort() 预处理候选集确保稳定):
Combination 1: [[1], [2], [3, 4, 5, 6]] Combination 2: [[1], [2, 3], [4, 5, 6]] ... Combination 10: [[1, 2, 3, 4], [5], [6]]
? 提示:若需支持自定义数组(如 int[] input),只需将 arr 初始化改为 Arrays.stream(input).boxed().collect(Collectors.toList()),其余逻辑完全复用。
掌握“分割点组合”这一建模范式,不仅能解决本题,还可推广至:子数组和约束、动态规划状态划分、字符串分词等经典场景。
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
正版软件
正版软件
正版软件
正版软件
正版软件
1
2
3
7
9