- 主题:这种组合算法怎么写?
a,b,c,d四个对象
我要生成a,b,c,d的全组合===C(4,1)+C(4,2)+C(4,3)+C(4,4)
结果就是输出 {a,b,c,d, ab,ac,ad,bc,bd,cd,abc,abd,acd,bcd,abcd}
这种算法很常见,java有类似的库吗?
--
修改:iStudy FROM 58.37.127.*
FROM 58.37.127.*
大神,能帮我看这修改一下吗?
package test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MySolution5 {
private static List<List<Integer[]>> helper(List<Integer[]> bookSet, int num) {
List<List<Integer[]>> ret = new ArrayList<>();
List<Integer[]> bookSetCombination = new ArrayList<>();
if (num > bookSet.size())
return ret;
if (num == 1) {
for (Integer[] book : bookSet) {
bookSetCombination.add(book.clone());
ret.add(bookSetCombination);
}
return ret;
}
if (num == bookSet.size()) {
for (Integer[] book : bookSet) {
bookSetCombination.add(book.clone());
}
ret.add(bookSetCombination);
return ret;
}
if (num < bookSet.size()) {
List<Integer[]> tmpBookSet = new ArrayList<>(bookSet);
for (int i = 0; i < bookSet.size(); i++) {
bookSetCombination.add(bookSet.get(i).clone());
//注释掉这句,是可以运行的
ret = helper(tmpBookSet, num - 1);
for (List<Integer[]> combination : ret) {
bookSetCombination.addAll(combination);
ret.add(bookSetCombination);
}
}
}
return ret;
}
public static void main(String[] args) {
// {3,10} 是一个book元素,看成一个元素就行了。
// 希望输出结果如下:
// {[3,10]} {[4,20]} {[1,5]}
// {[ 3, 10 ], [ 4, 20 ]} {[ 4, 20 ], [ 1, 5 ]} {[ 3, 10 ], [ 1, 5 ]}
// {[ 3, 10 ], [ 4, 20 ], [ 1, 5 ]}
Integer[][] books1 = new Integer[][] { { 3, 10 }, { 4, 20 }, { 1, 5 } };
List<Integer[]> bookCombination = new ArrayList<Integer[]>(Arrays.asList(books1));
int num = bookCombination.size();
while (num-- >= 0) {
List<List<Integer[]>> ret = helper(bookCombination, num);
for (List<Integer[]> combination : ret) {
for (Integer[] book : combination)
System.out.printf(" %s", Arrays.deepToString(book));
System.out.println();
}
;
}
}
}
【 在 callmebbser 的大作中提到: 】
: import java.util.Arrays;
: import java.util.TreeSet;
: public class CombinationTest {
: ...................
--
修改:iStudy FROM 58.37.127.*
FROM 58.37.127.*
这个helper函数代码少了几行。。我加在这里,我觉得思路很清晰,怎么编译不过呢
private static List<List<Integer[]>> helper(List<Integer[]> bookSet, int num) {
List<List<Integer[]>> ret = new ArrayList<>();
List<Integer[]> bookSetCombination=new ArrayList<>();
if(num>bookSet.size()) return ret ;
if(num==1) {
for (Integer[] book : bookSet)
{
bookSetCombination.add(book.clone());
ret.add(bookSetCombination);
}
return ret;
}
if (num == bookSet.size()) {
for (Integer[] book : bookSet)
{
bookSetCombination.add(book.clone());
}
ret.add(bookSetCombination);
return ret;
}
if (num < bookSet.size()) {
for (int i=0;i<bookSet.size();i++)
{
bookSetCombination.add(bookSet.get(i).clone());
List<Integer[]> tmpBookSet = new ArrayList<>(bookSet);
for(Integer[] tmpBook : tmpBookSet) {
if(tmpBook.equals(bookSet.get(i))) {
tmpBookSet.remove(tmpBook);
}
}
ret=helper(tmpBookSet, num-1);
for(List<Integer[]> combination :ret) {
bookSetCombination.addAll(combination);
ret.add(bookSetCombination);
}
}
}
return ret;
}
【 在 iStudy 的大作中提到: 】
: 大神,能帮我看这修改一下吗?
: package test;
: import java.util.ArrayList;
: ...................
--
修改:iStudy FROM 58.37.127.*
FROM 58.37.127.*
你这个程序太精炼了,不太看得懂。。
用你的例子改了一下,结果不对,哪里错了
package test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MySolution6 {
private static List<List<Integer[]>> helper(List<Integer[]> bookSet,int curIndex,int level, int num) {
List<List<Integer[]>> ret = new ArrayList<>();
List<Integer[]> bookSetCombination=new ArrayList<>();
if(num>bookSet.size()) return ret ;
if(num==1) {
for (Integer[] book : bookSet)
{
bookSetCombination.add(book.clone());
ret.add(bookSetCombination);
}
return ret;
}
if (num == (level+1)) {
for (Integer[] book : bookSet)
{
bookSetCombination.add(book.clone());
}
ret.add(bookSetCombination);
for (List<Integer[]> combination: ret) {
for (Integer[] book : combination)System.out.printf(" %s", Arrays.deepToString(book));
System.out.println();
};
return ret;
}
for (int i = curIndex; i < bookSet.size(); i++) {
bookSetCombination.add(bookSet.get(i));
ret=helper(bookSet, i + 1, level + 1, num);
}
return ret;
}
public static void main(String[] args) {
Integer[][] books1 = new Integer[][] { { 3, 10 }, { 4, 20 }, { 1,5 }, { 2, 40 },{3,40} };
List<Integer[]> bookSet = new ArrayList<Integer[]>(Arrays.asList(books1));
int num=bookSet.size();
while(num-->=0) {
List<List<Integer[]>> ret = helper(bookSet,0,0,num);
for (List<Integer[]> combination: ret) {
for (Integer[] book : combination)System.out.printf(" %s", Arrays.deepToString(book));
System.out.println();
};
}
}
}
【 在 callmebbser 的大作中提到: 】
: import java.util.Arrays;
: import java.util.TreeSet;
: public class CombinationTest {
: ...................
--
修改:iStudy FROM 58.37.127.*
FROM 58.37.127.*
有没有通用一点的样板程序。。。。我依猫画虎,试试
【 在 annals 的大作中提到: 】
: 本质就是所有的四位二进制数,1对应出现,0对应不出现
--
FROM 58.37.127.*
我的跟这个思路差不多
【 在 scy 的大作中提到: 】
: import java.util.ArrayList;
: import java.util.List;
: public class CombinationGenerator {
: ...................
--
FROM 58.37.127.*
赞,这个看上去不错。晚上研究一下
【 在 grayCat 的大作中提到: 】
: kimi这么说的:
: 在Java中,要生成对象的全组合,你可以使用`Stream` API配合`Collectors`来实现。Java标准库本身并没有直接提供组合库,但是你可以使用已有的库如Apache Commons Math或者Google Guava等来帮助生成组合。
: 以下是使用Java 8及以上版本Stream API实现的示例:
: ...................
--
FROM 58.37.127.*
你以为我抄袭的。。编译都没通过。。还要抄袭吗,,哈哈哈
【 在 presento 的大作中提到: 】
: 试试AI?楼上的是自己写的吗
: ?
--
FROM 58.37.127.*
说真的,他这个代码,不喜欢。 可读性非常差。最关键的level变量不知道啥意思
这种一般是数学系的人,做过一些变换后,写出来的。。。。
可读性差,可维护差,几年后,默写不出来的!
【 在 caravan 的大作中提到: 】
: 你还是看懂二楼比较好,
--
FROM 58.37.127.*
第三方库,选择不少。
apache也有。
【 在 lusio 的大作中提到: 】
: 爱学习没错,得善用工具。
: chatgpt回答:
: 是的,Java 中有许多库可以用于生成集合的所有组合。一个常用的库是 Google 的 Guava 库。Guava 提供了一个 Sets 类,其中包含了一些生成组合的方法。
: ...................
--
FROM 58.37.127.*