Java 如何使这种组合/置换方法递归?
我有一个字符串数组列表,希望将所有可能的组合存储到另一个集合中 例如:Java 如何使这种组合/置换方法递归?,java,recursion,permutation,combinations,Java,Recursion,Permutation,Combinations,我有一个字符串数组列表,希望将所有可能的组合存储到另一个集合中 例如: [air,bus,car] -> [air] [bus] [car] [air,bus] [air,car] [bus,air] [bus,car] [car,air] [car,bus] [air,bus,car] [air,car,bus] ... [car,bus,air] 重复并不重要。我现在的代码是: public ArrayList<String> comb(ArrayList<Stri
[air,bus,car]
->
[air]
[bus]
[car]
[air,bus]
[air,car]
[bus,air]
[bus,car]
[car,air]
[car,bus]
[air,bus,car]
[air,car,bus]
...
[car,bus,air]
重复并不重要。我现在的代码是:
public ArrayList<String> comb(ArrayList<String> wrds, ArrayList<String> str, int size)
{
ArrayList<String> s = new ArrayList<String>();
s.addAll(str);
if(size != a1.size())
{
Iterator e = a1.iterator();
while(e.hasNext())
{
s.add((String)e.next());
}
size++;
}
}
public ArrayList-comb(ArrayList-wrds、ArrayList-str、int-size)
{
ArrayList s=新的ArrayList();
s、 addAll(str);
如果(size!=a1.size())
{
迭代器e=a1.Iterator();
while(e.hasNext())
{
s、 添加((字符串)e.next());
}
大小++;
}
}
我试图让它递归地调用自己,这样它就可以存储这些组合。关于代码中缺少的位置或部分,是否可以获得帮助?将列表用作递归函数的参数。您可以使用一个新列表从函数内部调用该函数,该列表包含除第一项之外的所有内容。鉴于这是一个家庭作业,我将尝试为您提供答案的背景信息 解决这个问题的关键是使用递归 首先,假设数组中有两个项目。你可以删除第一个项目,给你的第一个组合。将剩余项目添加到第一个项目中会得到第二个组合。删除第二项将获得第三个组合。添加剩余的项将得到第四个组合。如果你有
[“air”,“bus”]
它会是这样的:
["air"]
["air", "bus"]
["bus"]
["bus", "air"]
public int multiply(int value1, int value2)
{
if (value1 > 1)
{
int remaining = value1 - 1;
return value2 + multiply(remaining, value2);
}
else
{
return value2;
}
}
public String[][] combinations(String[] strings)
{
if (strings.length > 1)
{
...
}
else
{
return new String[][]{strings};
}
}
public List<List<String>> combinations(List<String> strings)
{
if (strings.size()> 1)
{
...
}
else
{
List<List<String>> result = new ArrayList<List<String>>();
result.add(strings);
return result;
}
}
public List<List<String>> combinations(List<String> strings)
{
if (strings.size() > 1)
{
List<List<String>> result = new ArrayList<List<String>>();
for (String str : strings)
{
List<String> subStrings = new ArrayList<String>(strings);
subStrings.remove(str);
result.add(new ArrayList<String>(Arrays.asList(str)));
for (List<String> combinations : combinations(subStrings))
{
combinations.add(str);
result.add(combinations);
}
}
return result;
}
else
{
List<List<String>> result = new ArrayList<List<String>>();
result.add(new ArrayList<String>(strings));
return result;
}
}
返回如下内容的方法:
String[][] combinations(String[] strings)
需要注意的重要事项是,可以将包含单个字符串的数组传递给此方法,并且它可以返回包含包含单个字符串的数组的数组
这个问题有点复杂,因为您必须记录字符串组合,所以在解决这个问题之前,理解递归是很重要的
想象一下,你想写一个乘法方法,用两个数字相乘,但你只能使用加法和减法。您可以编写一个递归函数,将其中一个数字添加到自身,直到另一个数字达到退出条件,如:
["air"]
["air", "bus"]
["bus"]
["bus", "air"]
public int multiply(int value1, int value2)
{
if (value1 > 1)
{
int remaining = value1 - 1;
return value2 + multiply(remaining, value2);
}
else
{
return value2;
}
}
public String[][] combinations(String[] strings)
{
if (strings.length > 1)
{
...
}
else
{
return new String[][]{strings};
}
}
public List<List<String>> combinations(List<String> strings)
{
if (strings.size()> 1)
{
...
}
else
{
List<List<String>> result = new ArrayList<List<String>>();
result.add(strings);
return result;
}
}
public List<List<String>> combinations(List<String> strings)
{
if (strings.size() > 1)
{
List<List<String>> result = new ArrayList<List<String>>();
for (String str : strings)
{
List<String> subStrings = new ArrayList<String>(strings);
subStrings.remove(str);
result.add(new ArrayList<String>(Arrays.asList(str)));
for (List<String> combinations : combinations(subStrings))
{
combinations.add(str);
result.add(combinations);
}
}
return result;
}
else
{
List<List<String>> result = new ArrayList<List<String>>();
result.add(new ArrayList<String>(strings));
return result;
}
}
您可以对数组执行相同的操作,只在a值命中1
时退出,当数组包含一个项时退出,类似于:
["air"]
["air", "bus"]
["bus"]
["bus", "air"]
public int multiply(int value1, int value2)
{
if (value1 > 1)
{
int remaining = value1 - 1;
return value2 + multiply(remaining, value2);
}
else
{
return value2;
}
}
public String[][] combinations(String[] strings)
{
if (strings.length > 1)
{
...
}
else
{
return new String[][]{strings};
}
}
public List<List<String>> combinations(List<String> strings)
{
if (strings.size()> 1)
{
...
}
else
{
List<List<String>> result = new ArrayList<List<String>>();
result.add(strings);
return result;
}
}
public List<List<String>> combinations(List<String> strings)
{
if (strings.size() > 1)
{
List<List<String>> result = new ArrayList<List<String>>();
for (String str : strings)
{
List<String> subStrings = new ArrayList<String>(strings);
subStrings.remove(str);
result.add(new ArrayList<String>(Arrays.asList(str)));
for (List<String> combinations : combinations(subStrings))
{
combinations.add(str);
result.add(combinations);
}
}
return result;
}
else
{
List<List<String>> result = new ArrayList<List<String>>();
result.add(new ArrayList<String>(strings));
return result;
}
}
由于Java API的原因,使用Java.util.List
比使用数组更容易,因此您需要类似以下内容:
["air"]
["air", "bus"]
["bus"]
["bus", "air"]
public int multiply(int value1, int value2)
{
if (value1 > 1)
{
int remaining = value1 - 1;
return value2 + multiply(remaining, value2);
}
else
{
return value2;
}
}
public String[][] combinations(String[] strings)
{
if (strings.length > 1)
{
...
}
else
{
return new String[][]{strings};
}
}
public List<List<String>> combinations(List<String> strings)
{
if (strings.size()> 1)
{
...
}
else
{
List<List<String>> result = new ArrayList<List<String>>();
result.add(strings);
return result;
}
}
public List<List<String>> combinations(List<String> strings)
{
if (strings.size() > 1)
{
List<List<String>> result = new ArrayList<List<String>>();
for (String str : strings)
{
List<String> subStrings = new ArrayList<String>(strings);
subStrings.remove(str);
result.add(new ArrayList<String>(Arrays.asList(str)));
for (List<String> combinations : combinations(subStrings))
{
combinations.add(str);
result.add(combinations);
}
}
return result;
}
else
{
List<List<String>> result = new ArrayList<List<String>>();
result.add(new ArrayList<String>(strings));
return result;
}
}
总之,您要做的是将字符串列表缩减为单个项,然后将其与前面的项组合,以在线程返回调用堆栈时生成所有可能的组合。public static void composition(Object[]array){
public static void combination(Object[] array){
for(int x = 0; x < (1 << array.length); x++){
System.out.print("[");
for(int y = 0; y < array.length; y++){
if(checkIsOn(x, y){
System.out.print(array[y]);
}
}
System.out.println("]");
}
}
public static boolean checkIsOn(int mast, int position){
return (mast & (1 << position) > 0);
}
对于(int x=0;x<(1)如果这是作业,则将其标记为作业。a1来自何处?假设它是作业。变量a1是[air,bus,car]的数组列表.你错过了递归调用本身。是的,这就是我寻求帮助的原因。我很困惑。我认为家庭作业标记已被弃用。但我仍然不确定如何将它们添加到arraylist中,或者甚至是arraylist的数组中。你的解释非常清楚。非常感谢!我感觉我需要一个列表列表,但是我不知道如何使用它。回到递归,我开始更好地理解它。我很感激它,尼克!不客气,不过没有什么比向上投票并接受答案更能表达感谢的了。很抱歉。虽然我不能向上投票,因为我没有超过15票,但我接受了答案。谢谢提醒。什么我一直在寻找。很好的答案尼克这似乎输出了一个超集。但我不明白这是如何工作的。你/某人能把它分解一下吗?