以下是我的要求
将单词加入所有可能的排列并将其存储在一个 列表或数组
我尝试了什么
public class Example {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
Example e = new Example();
String changeCase = "ab";
String result = changeCase.toLowerCase();
list = e.permutation("", result);
System.out.println("list size is "+list.size());
for (String str : list) {
System.out.println(str);
}
}
private ArrayList<String> permutation(String prefix, String str) {
ArrayList<String> l = new ArrayList<String>();
int n = str.length();
if (n == 0) {
System.out.println(prefix);
l.add(prefix);
} else {
for (int i = 0; i < n; i++) {
permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i + 1, n));
}
}
return l;
}
}
输出
ab
ba
list size is 0
我确保将所有值添加到列表中,但是当我检查列表大小时,它显示为零。
答案 0 :(得分:1)
你需要在列表中添加递归调用的响应(即包含排列的响应列表)
l.addAll(permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i + 1, n)));
答案 1 :(得分:1)
您为ArrayList
的每次递归调用创建一个新的permutations
,而不是每次都重复使用相同的列表。
可能的解决方案:
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<String>();
Example e = new Example();
e.permutation(list, "", result);
...
}
private void permutation(List<String> list, String prefix, String str) {
int n = str.length();
if (n == 0) {
System.out.println(prefix);
list.add(prefix);
} else {
for (int i = 0; i < n; i++) {
permutation(list, prefix + str.charAt(i), str.substring(0, i) + str.substring(i + 1, n));
}
}
}
答案 2 :(得分:0)
您忘记捕获递归结果
for (int i = 0; i < n; i++) {
l.addAll(permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i + 1, n))));
}
答案 3 :(得分:0)
我已经对您的代码进行了评论,以指出您第一次调用此方法时发生的事情(前缀=&#34;&#34;和str =&#34; ab&#34;):
private ArrayList<String> permutation(String prefix, String str) {
ArrayList<String> l = new ArrayList<String>(); //Create a new empty list
int n = str.length(); //obtain the length of str ("ab") so n=2
if (n == 0) { //wrong cause n=2
System.out.println(prefix); //does not execute cause n=2
l.add(prefix); //does not execute cause n=2
} else { //execute the else cause if condition is false
for (int i = 0; i < n; i++) { //for each character in str
permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i + 1, n)); //call a function and ignore the result
}
} //end of else
return l; //return l, but since nothing was added to l, l is the empty list created at the beginning => l.size()=0
}
递归中的问题是每次都重新创建列表并忽略子步骤的结果。您需要捕获子步骤的结果并将其添加到列表中。
private ArrayList<String> permutation(String prefix, String str) {
ArrayList<String> l = new ArrayList<String>();
int n = str.length();
if (n == 0) {
System.out.println(prefix);
l.add(prefix);
} else {
for (int i = 0; i < n; i++) {
l.addAll(permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i + 1, n))); //add the result of the sub-step to the list
}
}
return l;
}
答案 4 :(得分:0)
就个人而言,我会将列表作为类成员而不是
public class Example {
private List<String> result = new ArrayList<String>();
public List<String> getResult() {
return result;
}
private void permutation(String prefix, String str) {
int n = str.length();
if (n == 0) {
result.add(prefix);
} else {
for (int i = 0; i < n; i++) {
permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i + 1, n));
}
}
}
public static void main(String[] args) {
Example e = new Example();
String scramble = "abc";
e.permutation("", scramble);
for (String str : e.getResult()) {
System.out.println(str);
}
}
}