[英]Finding all subsets of a list of strings with recursion no loop
Is it possible to find all the subsets of a string list using only recursion and no loops?是否可以仅使用递归而不使用循环来查找字符串列表的所有子集? Would this require multiple methods?
这需要多种方法吗?
The below Solution is not suitable as it finds only a subset of the sublists, still leaving it here as it may help the author.下面的解决方案不合适,因为它只找到了子列表的一个子集,仍然把它留在这里,因为它可能对作者有帮助。
This would be a solution:这将是一个解决方案:
public void findSubsets(List<String> list, int listSize)
{
if(listSize <= 1)
{
System.out.println(list.subList(0, 1));
return;
}
System.out.println(list.subList(0, listSize));
findSubsets(list, listSize - 1);
}
Or if you need to return the Sub Lists:或者,如果您需要返回子列表:
public List<List<String>> findSubsets(List<String> list, int listSize, List<List<String>> subLists)
{
if(listSize <= 1)
{
subLists.add(list.subList(0, 1));
return subLists;
}
subLists.add(list.subList(0, listSize));
return findSubsets(list, listSize - 1, subLists);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.