我有一个字符串数组
{"ted", "williams", "golden", "voice", "radio"}
我希望以下表格中包含这些关键字的所有可能组合:
{"ted",
"williams",
"golden",
"voice",
"radio",
"ted williams",
"ted golden",
"ted voice",
"ted radio",
"williams golden",
"williams voice",
"williams radio",
"golden voice",
"golden radio",
"voice radio",
"ted williams golden",
"ted williams voice",
"ted williams radio",
.... }
我已经好几个小时都没有有效的结果(高级编程的副作用?)。
我知道解决方案应该是显而易见的,但老实说,我卡住了! Java / C#中的解决方案被接受。
修改:
编辑2 :在回答答案中的链接后,发现Guava用户可以在com.google.common.collect.Sets中使用powerset方法
答案 0 :(得分:17)
编辑:正如FearUs所指出的,更好的解决方案是使用Guava的Sets.powerset(Set set)。
编辑2:更新了链接。
this solution的快速而肮脏的翻译:
public static void main(String[] args) {
List<List<String>> powerSet = new LinkedList<List<String>>();
for (int i = 1; i <= args.length; i++)
powerSet.addAll(combination(Arrays.asList(args), i));
System.out.println(powerSet);
}
public static <T> List<List<T>> combination(List<T> values, int size) {
if (0 == size) {
return Collections.singletonList(Collections.<T> emptyList());
}
if (values.isEmpty()) {
return Collections.emptyList();
}
List<List<T>> combination = new LinkedList<List<T>>();
T actual = values.iterator().next();
List<T> subSet = new LinkedList<T>(values);
subSet.remove(actual);
List<List<T>> subSetCombination = combination(subSet, size - 1);
for (List<T> set : subSetCombination) {
List<T> newSet = new LinkedList<T>(set);
newSet.add(0, actual);
combination.add(newSet);
}
combination.addAll(combination(subSet, size));
return combination;
}
测试:
$ java PowerSet ted williams golden
[[ted], [williams], [golden], [ted, williams], [ted, golden], [williams, golden], [ted, williams, golden]]
$
答案 1 :(得分:3)
这是一个提示:
All-Subsets(X) = {union for all y in X: All-Subsets(X-y)} union {X}
答案 2 :(得分:2)
我的优化解决方案基于Matthew McPeak提供的解决方案。此版本避免了不必要的数组副本。
public static <T> T[][] combinations(T[] a) {
int len = a.length;
if (len > 31)
throw new IllegalArgumentException();
int numCombinations = (1 << len) - 1;
@SuppressWarnings("unchecked")
T[][] combinations = (T[][]) java.lang.reflect.Array.newInstance(a.getClass(), numCombinations);
// Start i at 1, so that we do not include the empty set in the results
for (int i = 1; i <= numCombinations; i++) {
@SuppressWarnings("unchecked")
T[] combination = (T[]) java.lang.reflect.Array.newInstance(a.getClass().getComponentType(),
Integer.bitCount(i));
for (int j = 0, ofs = 0; j < len; j++)
if ((i & (1 << j)) > 0)
combination[ofs++] = a[j];
combinations[i - 1] = combination;
}
return combinations;
}
答案 3 :(得分:1)
package rnd;
import java.util.ArrayList;
public class Rnd {
public static void main(String args[]) {
String a[] = {"ted", "williams", "golden", "voice", "radio"};
ArrayList<String> result =new ArrayList<>();
for(int i =0 ;i< a.length; i++){
String s = "";
for(int j =i ; j < a.length; j++){
s += a[j] + " " ;
result.add(s);
}
}
}
}
答案 4 :(得分:0)
我知道这个问题已经过时了,但我没有找到满足我需求的答案。因此,使用权力集的概念,以及番石榴库的有序排列,我能够获得原始数组中所有元素组合的数组。
我想要的是:
如果我有一个包含三个字符串的数组
ArrayList<String> tagsArray = new ArrayList<>(Array.asList("foo","bar","cas"));
我希望在数组中包含所有元素的可能组合:
{"foo","bar","cas","foobar","foocas","barfoo","barcas","casfoo","casbar","foobarcas","casbarfoo","barcasfoo" . . . . . }
因此,为了获得这个结果,我使用google的guava lib实现了下一个代码:
import static com.google.common.collect.Collections2.orderedPermutations;
import static java.util.Arrays.asList;
public void createTags(){
Set<String> tags = new HashSet<>();
tags.addAll(tagsArray);
Set<Set<String>> tagsSets = Sets.powerSet(tags);
for (Set<String> sets : tagsSets) {
List<String> myList = new ArrayList<>();
myList.addAll(sets);
if (!myList.isEmpty()) {
for (List<String> perm : orderedPermutations(myList)) {
System.out.println(perm);
String myTag = Joiner.on("").join(perm);
tagsForQuery.add(myTag);
}
}
}
for (String hashtag : tagsForQuery) {
System.out.println(hashtag);
}
}
我希望这对某人有所帮助,这不是为了做作业,而是为了一个Android应用程序。
答案 5 :(得分:0)
import java.util.ArrayList;
import java.util.List;
public class AllPossibleCombinations {
public static void main(String[] args) {
String[] a={"ted", "williams", "golden"};
List<List<String>> list = new AllPossibleElementCombinations().getAllCombinations(Arrays.asList(a));
for (List<String> arr:list) {
for(String s:arr){
System.out.print(s);
}
System.out.println();
}
}
public List<List<String>> getAllCombinations(List<String> elements) {
List<List<String>> combinationList = new ArrayList<List<String>>();
for ( long i = 1; i < Math.pow(2, elements.size()); i++ ) {
List<String> list = new ArrayList<String>();
for ( int j = 0; j < elements.size(); j++ ) {
if ( (i & (long) Math.pow(2, j)) > 0 ) {
list.add(elements.get(j));
}
}
combinationList.add(list);
}
return combinationList;
}
}
输出:
ted
威廉斯
泰德威廉姆斯
金色
tedgolden
williamsgolden
tedwilliamsgolden