Java 递归地获取单词的组合
我已经为此挣扎了好几天,但我似乎不能 完成它。问题如下: 编写一个名为String[]perm(int n)的递归方法,该方法接受 一个参数:整数n。该方法返回所有单词的数组 有n个音节。可供使用的词语有:“Foo” 和“酒吧” 我有以下没有递归的代码:Java 递归地获取单词的组合,java,recursion,permutation,Java,Recursion,Permutation,我已经为此挣扎了好几天,但我似乎不能 完成它。问题如下: 编写一个名为String[]perm(int n)的递归方法,该方法接受 一个参数:整数n。该方法返回所有单词的数组 有n个音节。可供使用的词语有:“Foo” 和“酒吧” 我有以下没有递归的代码: static String[] words = {"Foo","Bar"}; static int n = 2; static int count = 0; public static String[] perm
static String[] words = {"Foo","Bar"};
static int n = 2;
static int count = 0;
public static String[] perm(int n) {
String[] wordsArray = new String[4];
for(int i = 0; i < words.length; i++) {
for(int j = 0; j < words.length; j++) {
wordsArray[count] = words[i] + words[j];
count++;
}
}
return wordsArray;
}
这里是一个初步的方法,请修改它根据您的需要
public static void permutationForAString(String str) {
permutation("", str);
}
private static void permutation(String prefix, String str) {
int n = str.length();
if (n == 0) System.out.println(prefix);
else {
for (int i = 0; i < n; i++)
permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i+1, n));
}
}
将输出为
ABC
ACB
BAC
BCA
CAB
CBA
这里是一个初步的方法,请修改它根据您的需要
public static void permutationForAString(String str) {
permutation("", str);
}
private static void permutation(String prefix, String str) {
int n = str.length();
if (n == 0) System.out.println(prefix);
else {
for (int i = 0; i < n; i++)
permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i+1, n));
}
}
将输出为
ABC
ACB
BAC
BCA
CAB
CBA
这是一些你需要的东西
public List<String> permute(String[] stringInput, int curr)
{
List<String> permutations = new ArrayList<String>();
if (curr >= stringInput.length-1)
{
return Arrays.asList(stringInput);
}
else
{
for (int i = 0; i < stringInput.length; i++)
{
String currentCharacter = stringInput[i];
List<String> permutationsOutput = permute(stringInput,curr+1);
for (String singlePermutation : permutationsOutput)
{
String currentPermutations = currentCharacter + singlePermutation;
permutations.add(currentPermutations);
}
}
return permutations;
}
}
其中0
是开始排列的起始索引,它也将作为递归情况下的限制条件
输出::
FooFoo
FooBar
BarFoo
BarBar
这是一些你需要的东西
public List<String> permute(String[] stringInput, int curr)
{
List<String> permutations = new ArrayList<String>();
if (curr >= stringInput.length-1)
{
return Arrays.asList(stringInput);
}
else
{
for (int i = 0; i < stringInput.length; i++)
{
String currentCharacter = stringInput[i];
List<String> permutationsOutput = permute(stringInput,curr+1);
for (String singlePermutation : permutationsOutput)
{
String currentPermutations = currentCharacter + singlePermutation;
permutations.add(currentPermutations);
}
}
return permutations;
}
}
其中0
是开始排列的起始索引,它也将作为递归情况下的限制条件
输出::
FooFoo
FooBar
BarFoo
BarBar
嗯,我试了很多,但都没用或是缺少什么。上面的函数给出了它应该做什么的最佳表示,即使它不是递归的。你的代码不适用于3个字符串,你也需要解决这个问题。好吧,这就是问题所在,对于3个音节,循环2³=3。这是应该是递归的部分,但我无法让递归函数附加字符串并返回字符串数组。我正在重建我以前的函数,并将以这种方式对其进行编辑:所有
n
音节排列的递归定义都是n-1
音节排列,并附加Foo
或Bar
。用代码写出来。我试了很多,但都没用或是缺少什么。上面的函数给出了它应该做什么的最佳表示,即使它不是递归的。你的代码不适用于3个字符串,你也需要解决这个问题。好吧,这就是问题所在,对于3个音节,循环2³=3。这是应该是递归的部分,但我无法让递归函数附加字符串并返回字符串数组。我正在重建我以前的函数,并将以这种方式对其进行编辑:所有n
音节排列的递归定义都是n-1
音节排列,并附加Foo
或Bar
。用代码写。