- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在寻求帮助来调整 Java 代码,该代码从一组整数生成所有分区。我希望修改代码(我在 Stackoverflow 上找到)以仅生成 3 个子集的分区。
示例:
我的列表包含 1, 2, 3, 4, 5, 6
我想要获得 3 个分区,即我想要将列表分为 3 个子集。
输出应如下所示:
[1, 2, 3], [4], [5, 6]
[1, 2], [3, 5], [4, 6]
等等...
我试图修改这段代码,但没有成功。如果我生成所有分区,则仅考虑划分为 3 个子列表的分区。这需要时间。所以我希望避免生成不必要的分区。
代码链接:
Get all possible partitions of a set
package PartitionSetCreator;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
public class PartitionSetCreator<T> {
private Set<Set<Set<T>>> parts;//the partitions that are created
private Set<Set<T>> pow;//the power set of the input set
private Set<T> base;//the base set
/**
* The main method is just for testing and can be deleted.
*/
public static void main(String[] args) {
//test using an empty set = []
Set<Integer> baseSet = new HashSet<Integer>();
for (int i = 1; i < 10; i++) {
baseSet.add(i);
}
System.out.println("BaseSet: " + baseSet);
PartitionSetCreator<Integer> partSetCreator5 = new PartitionSetCreator<Integer>(baseSet);
Set<Set<Set<Integer>>> partitionSets5 = partSetCreator5.findAllPartitions();
System.out.println("Result: " + partitionSets5);
Iterator<Set<Set<Integer>>> iter = partitionSets5.iterator();
Set<Set<Set<Integer>>> partitionSetsof3 = new HashSet<Set<Set<Integer>>>();
//Print the output
for (Set<Set<Integer>> stock : partitionSets5) {
//Extract when the size is equal to 3
if (stock.size() == 3) {
partitionSetsof3.add(stock);
System.out.println(stock);
}
}
System.out.println(partitionSetsof3);
System.out.println("Base-Size: " + baseSet.size() + " Result-Size: " + partitionSets5.size());
}
public PartitionSetCreator(Set<T> base) {
this.base = base;
this.pow = powerSet(base);
if (pow.size() > 1) {
//remove the empty set if it's not the only entry in the power set
pow.remove(new HashSet<T>());
}
this.parts = new HashSet<Set<Set<T>>>();
}
/**
* Calculation is in this method.
*/
public Set<Set<Set<T>>> findAllPartitions() {
//find part sets for every entry in the power set
for (Set<T> set : pow) {
Set<Set<T>> current = new HashSet<Set<T>>();
current.add(set);
findPartSets(current);
}
//return all partitions that were found
return parts;
}
/**
* Finds all partition sets for the given input and adds them to parts (global variable).
*/
private void findPartSets(Set<Set<T>> current) {
int maxLen = base.size() - deepSize(current);
if (maxLen == 0) {
//the current partition is full -> add it to parts
parts.add(current);
//no more can be added to current -> stop the recursion
return;
} else {
//for all possible lengths
for (int i = 1; i <= maxLen; i++) {
//for every entry in the power set
for (Set<T> set : pow) {
if (set.size() == i) {
//the set from the power set has the searched length
if (!anyInDeepSet(set, current)) {
//none of set is in current
Set<Set<T>> next = new HashSet<Set<T>>();
next.addAll(current);
next.add(set);
//next = current + set
findPartSets(next);
}
}
}
}
}
}
/**
* Creates a power set from the base set.
*/
private Set<Set<T>> powerSet(Set<T> base) {
Set<Set<T>> pset = new HashSet<Set<T>>();
if (base.isEmpty()) {
pset.add(new HashSet<T>());
return pset;
}
List<T> list = new ArrayList<T>(base);
T head = list.get(0);
Set<T> rest = new HashSet<T>(list.subList(1, list.size()));
for (Set<T> set : powerSet(rest)) {
Set<T> newSet = new HashSet<T>();
newSet.add(head);
newSet.addAll(set);
pset.add(newSet);
pset.add(set);
}
return pset;
}
/**
* The summed up size of all sub-sets
*/
private int deepSize(Set<Set<T>> set) {
int deepSize = 0;
for (Set<T> s : set) {
deepSize += s.size();
}
return deepSize;
}
/**
* Checks whether any of set is in any of the sub-sets of current
*/
private boolean anyInDeepSet(Set<T> set, Set<Set<T>> current) {
boolean containing = false;
for (Set<T> s : current) {
for (T item : set) {
containing |= s.contains(item);
}
}
return containing;
}
}
期望避免生成不必要的分区以加速运行。
最佳答案
static void splitArray(int[] arr) {
int i, j, k;
int n = arr.length;
for (i = 1; i < n; i++) {
for (j = 1; j < n; j++) {
for (k = 1; k < n; k++) {
if (i + j + k == n) {
System.out.println( //
Arrays.toString(Arrays.copyOfRange(arr, 0, i)) + ", " + //
Arrays.toString(Arrays.copyOfRange(arr, i, i + j)) + ", " + //
Arrays.toString(Arrays.copyOfRange(arr, i + j, n)) //
);
}
}
}
}
}
public static void main(String[] args) {
int[] arr = { 1, 2, 3, 4, 5, 6 };
splitArray(arr);
}
输出
[1], [2], [3, 4, 5, 6]
[1], [2, 3], [4, 5, 6]
[1], [2, 3, 4], [5, 6]
[1], [2, 3, 4, 5], [6]
[1, 2], [3], [4, 5, 6]
[1, 2], [3, 4], [5, 6]
[1, 2], [3, 4, 5], [6]
[1, 2, 3], [4], [5, 6]
[1, 2, 3], [4, 5], [6]
[1, 2, 3, 4], [5], [6]
一般情况下,使用CollectionUtils.permutations查找 arr
的所有排列并为每个排列调用 splitArray()
关于java - 如何仅生成整数列表的 3 个分区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58126202/
是一种在 Neo4j 分区之间进行物理分离的方法吗? 这意味着以下查询将转到 node1: Match (a:User:Facebook) 虽然此查询将转到另一个节点(可能托管在 docker 上)
我尝试在我的 SQL 服务器上使用分区函数对我的一个大表进行分区,但我收到一条错误消息 “只能在SQL Server企业版中创建分区功能。只有SQL Server企业版支持分区。” 所以我想知道没有企
在hadoop文件系统中,我有两个文件,分别是X和Y。通常,hadoop制作的文件X和Y的大小为64 MB。是否可以强制hadoop划分两个文件,以便从X的32 MB和Y的32 MB中创建一个64 M
据我了解,如果我们有一个主键,则使用该键对数据进行分区并将其存储在节点中(例如使用随机分区器)。 现在我不确定的是,如果我有多个键(又名复合键),是用于分区数据的键的组合还是它将是第一个主键? 例如,
我正在向我的 SSAS 多维数据集添加分区,我想知道是否有多个分区可以保留在下面?多少太多了,最佳实践限制是 20 还是 200?有没有人可以分享任何真实世界的知识? 最佳答案 这是 another
我有一个包含大约 200 万条记录的大表,我想对其进行分区。 我将 id 列设置为 PRIMARY AUTO_INCRMENT int (并且它必须始终是唯一的)。我有一列“theyear”int(4
我正在做 mysql 列表分区。我的表数据如下 ---------------------------------------- id | unique_token | city | student_
我有一个表,我们每天在其中插入大约 2000 万个条目(没有任何限制的盲插入)。我们有两个外键,其中一个是对包含大约 1000 万个条目的表的引用 ID。 我打算删除此表中超过一个月的所有数据,因为不
我想在一款足球奇幻游戏中尝试使用 MySQL Partitioning,该游戏的用户分布在联赛中,每个联赛都有一个用户可以买卖球员的市场。当很多用户同时玩时,我在这张表中遇到了一些僵局(在撰写本文时大
我是 jQuery 的新手,想知道是否可以获取一些变量并将它们的除法作为 CSS 宽度。到目前为止我在这里: var x = $(".some-container").length; var y =
所以我正在做家庭作业,我需要为分区、斯特林数(第一类和第二类)和第一类的切比雪夫多项式创建递归函数。我的程序应该能够让用户输入一个正整数 n,然后创建名为 Partitions.txt、Stirlin
我在数据框中有一列,其中包含大约 1,4M 行聊天对话,其中每个单元格中的一般格式为 (1): “名称代理 : 对话” 但是,并非列中的所有单元格都采用这种格式。有些单元格只是 (2): “对话” 我
我在尝试隐藏 a 时遇到了一些问题,直到用户单击某个元素为止。 HTML 看起来像: BRAND item 1 item 2 item 3
一.为什么kafka要做分区? 因为当一台机器有可能扛不住(类比:就像redis集群中的redis-cluster一样,一个master抗不住写,那么就多个master去抗写)
我有一些销售数据,我需要发送存储在单独表中的可用槽中的数量。 销售数据示例: id数量112131415369 create table sales (id serial primary key, q
我计划设置多个节点以使用 glusterfs 创建分布式复制卷 我使用主(也是唯一)分区上的目录在两个节点上创建了一个 gluster 复制卷。 gluster volume create vol_d
我正在尝试使用 sum() over (partition by) 但在总和中过滤。我的用例是将每个产品的 12 个月累计到一个月的条目,因此: ITEM MONTH SALES Item
是否可以创建多个 Enumerators出单Enumerator ? 我正在寻找的相当于 List.partition返回 (List[A], List[A]) ,比如 List().partitio
我正在创建一个基于 x86 的非常简单的 Yocto 图像。 我希望/文件系统是只读的,所以我设置了 IMAGE_FEATURES_append = " read-only-rootfs " 在原件的
是否可以使用一次 collect 调用来创建 2 个新列表?如果没有,我该如何使用分区来做到这一点? 最佳答案 collect(在TraversableLike上定义并在所有子类中可用)与集合和Par
我是一名优秀的程序员,十分优秀!