首页 / JAVA / java-将n大小的数组拆分为k个框
java-将n大小的数组拆分为k个框
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-将n大小的数组拆分为k个框,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含6186字,纯文字阅读大概需要9分钟。
内容图文
我正在尝试编写一个程序,以递增的顺序给出n个整数数组和k个框,它将原始数组拆分为连续数字的框,即按输入顺序显示
到目前为止,我已经编写了以下代码
int[] A = {1,2,3,4,5};
int k = 3;
int n = 5;
for(int i = 0; i <= n - k; i++){
for(int j = i+1; j < n-1; j++){
int[] k1 = Arrays.copyOfRange(A, 0, i+1);
int[] k2 = Arrays.copyOfRange(A, i+1, j+1);
int[] k3 = Arrays.copyOfRange(A, j+1, n);
System.out.println(Arrays.toString(k1) + "|" + Arrays.toString(k2) + "|" + Arrays.toString(k3));
}
}
但是,我的代码存在的问题是我已经对循环和k-box进行了硬编码,但不确定如何解决问题.
该功能的目标是生成在每个框中放置元素的所有可能性.
感谢您提供有关该算法的帮助或想法!
解决方法:
搜索时,我遇到了this answer.您可以使用它来获取输入列表的所有可能分区.您必须对该代码进行一些小的修改:holder.addAll(b);变为holder.addAll(0,b);,因此b的值添加在前面而不是结尾,这意味着原始顺序主要保留而不是颠倒.
之后,您可以使用两个过滤器:
>检查一个(展平的)分区中的所有值是否仍保持原始顺序,删除所有不存在的值.
>并根据块k的大小对其进行过滤(为此我使用了Java 8流过滤器).
这里是一个可能的解决方案:
import java.util.*;
import java.util.stream.Collectors;
class Main{
public static void main(String[] args) {
int[] A = {1,2,3,4,5};
// Convert your int-array to an Integer-ArrayList:
List<Integer> inputList = Arrays.stream(A).boxed().collect(Collectors.toList());
// Get all possible partitions of this List:
List<List<List<Integer>>> partitions = partition(inputList);
System.out.println("All partitions: ");
prettyPrintPartitions(partitions);
// Remove all items which aren't in the original order:
filterPartitionsByOriginalOrder(partitions, A);
System.out.println("\nPartitions that are in the original order: ");
prettyPrintPartitions(partitions);
// Filter them based on amount of chunks `k`:
for(int k=2; k<A.length; k++){
System.out.println("\nPartitions of size "+k+" (and also in the original order): ");
List<List<List<Integer>>> filteredPartitions = filterPartitionsByAmountOfChunks(partitions, k);
prettyPrintPartitions(filteredPartitions);
}
}
private static void prettyPrintPartitions(List<List<List<Integer>>> partitions){
for(List<List<Integer>> partition : partitions){
System.out.println(partition);
}
}
/* Method to get all partitions (all possible ways to divide the list in a variable amount of chunks) of a List of Integers */
private static List<List<List<Integer>>> partition(List<Integer> inputList) {
List<List<List<Integer>>> result = new ArrayList<>();
if(inputList.isEmpty()){
List<List<Integer>> empty = new ArrayList<>();
result.add(empty);
return result;
}
// Note that this algorithm only works if inputList.size() < 31
// since you overflow int space beyond that. This is true even
// if you use Math.pow and cast back to int.
int limit = 1 << (inputList.size() - 1);
// Note the separate variable to avoid resetting
// the loop variable on each iteration.
for(int j=0; j<limit; j++){
List<List<Integer>> parts = new ArrayList<>();
List<Integer> part1 = new ArrayList<>();
List<Integer> part2 = new ArrayList<>();
parts.add(part1);
parts.add(part2);
int i = j;
for(Integer item : inputList){
parts.get(i%2).add(item);
i >>= 1;
}
for(List<List<Integer>> b : partition(part2)){
List<List<Integer>> holder = new ArrayList<>();
holder.add(part1);
// Add them at the start instead of end so the items hold the original order
holder.addAll(0, b);
result.add(holder);
}
}
return result;
}
/* Method to filter a List of List of List of Integers (partitions) based on a given amount of chunks `k` */
private static List<List<List<Integer>>> filterPartitionsByAmountOfChunks(List<List<List<Integer>>> partitions, int k){
List<List<List<Integer>>> filteredPartitions = partitions.stream()
.filter(partition -> partition.size() == k)
.collect(Collectors.toList());
return filteredPartitions;
}
/* Method to remove any partition that (flattened) isn't in the same order as the original given int-array */
private static void filterPartitionsByOriginalOrder(List<List<List<Integer>>> partitions, int[] A){
partitions.removeIf(partition -> {
int index = 0;
for(List<Integer> part : partition){
for(int value : part){
// The value is not at the same index in the original array,
// so remove the partition
if(value != A[index]){
return true;
}
index++;
}
}
return false;
});
}
}
哪个输出:
All partitions:
[[1, 2, 3, 4, 5]]
[[1], [2, 3, 4, 5]]
[[2], [1, 3, 4, 5]]
[[1, 2], [3, 4, 5]]
[[1], [2], [3, 4, 5]]
[[3], [1, 2, 4, 5]]
[[1, 3], [2, 4, 5]]
[[1], [3], [2, 4, 5]]
[[2, 3], [1, 4, 5]]
[[2], [3], [1, 4, 5]]
[[1, 2, 3], [4, 5]]
[[1], [2, 3], [4, 5]]
[[2], [1, 3], [4, 5]]
[[1, 2], [3], [4, 5]]
[[1], [2], [3], [4, 5]]
[[4], [1, 2, 3, 5]]
[[1, 4], [2, 3, 5]]
[[1], [4], [2, 3, 5]]
[[2, 4], [1, 3, 5]]
[[2], [4], [1, 3, 5]]
[[1, 2, 4], [3, 5]]
[[1], [2, 4], [3, 5]]
[[2], [1, 4], [3, 5]]
[[1, 2], [4], [3, 5]]
[[1], [2], [4], [3, 5]]
[[3, 4], [1, 2, 5]]
[[3], [4], [1, 2, 5]]
[[1, 3, 4], [2, 5]]
[[1], [3, 4], [2, 5]]
[[3], [1, 4], [2, 5]]
[[1, 3], [4], [2, 5]]
[[1], [3], [4], [2, 5]]
[[2, 3, 4], [1, 5]]
[[2], [3, 4], [1, 5]]
[[3], [2, 4], [1, 5]]
[[2, 3], [4], [1, 5]]
[[2], [3], [4], [1, 5]]
[[1, 2, 3, 4], [5]]
[[1], [2, 3, 4], [5]]
[[2], [1, 3, 4], [5]]
[[1, 2], [3, 4], [5]]
[[1], [2], [3, 4], [5]]
[[3], [1, 2, 4], [5]]
[[1, 3], [2, 4], [5]]
[[1], [3], [2, 4], [5]]
[[2, 3], [1, 4], [5]]
[[2], [3], [1, 4], [5]]
[[1, 2, 3], [4], [5]]
[[1], [2, 3], [4], [5]]
[[2], [1, 3], [4], [5]]
[[1, 2], [3], [4], [5]]
[[1], [2], [3], [4], [5]]
Partitions that are in the original order:
[[1, 2, 3, 4, 5]]
[[1], [2, 3, 4, 5]]
[[1, 2], [3, 4, 5]]
[[1], [2], [3, 4, 5]]
[[1, 2, 3], [4, 5]]
[[1], [2, 3], [4, 5]]
[[1, 2], [3], [4, 5]]
[[1], [2], [3], [4, 5]]
[[1, 2, 3, 4], [5]]
[[1], [2, 3, 4], [5]]
[[1, 2], [3, 4], [5]]
[[1], [2], [3, 4], [5]]
[[1, 2, 3], [4], [5]]
[[1], [2, 3], [4], [5]]
[[1, 2], [3], [4], [5]]
[[1], [2], [3], [4], [5]]
Partitions of size 2 (and also in the original order):
[[1], [2, 3, 4, 5]]
[[1, 2], [3, 4, 5]]
[[1, 2, 3], [4, 5]]
[[1, 2, 3, 4], [5]]
Partitions of size 3 (and also in the original order):
[[1], [2], [3, 4, 5]]
[[1], [2, 3], [4, 5]]
[[1, 2], [3], [4, 5]]
[[1], [2, 3, 4], [5]]
[[1, 2], [3, 4], [5]]
[[1, 2, 3], [4], [5]]
Partitions of size 4 (and also in the original order):
[[1], [2], [3], [4, 5]]
[[1], [2], [3, 4], [5]]
[[1], [2, 3], [4], [5]]
[[1, 2], [3], [4], [5]]
内容总结
以上是互联网集市为您收集整理的java-将n大小的数组拆分为k个框全部内容,希望文章能够帮你解决java-将n大小的数组拆分为k个框所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。