无法理解字符串置换Java代码
我有这个工作代码,可以不重复地打印字符串排列,但我无法理解它在逻辑中是如何工作的。任何建议都会很有帮助无法理解字符串置换Java代码,java,string,permutation,Java,String,Permutation,我有这个工作代码,可以不重复地打印字符串排列,但我无法理解它在逻辑中是如何工作的。任何建议都会很有帮助 private static void permutation(String input, String sofar) { if (input.equals("")) { System.out.println(count + " " + sofar); count++; } for (int i =
private static void permutation(String input, String sofar) {
if (input.equals("")) {
System.out.println(count + " " + sofar);
count++;
}
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (input.indexOf(c, i + 1) != -1)
continue;
permutation(input.substring(0, i) + input.substring(i + 1), sofar+c);
}
}
for(int i=0;i
上面的for循环正在发挥神奇的作用
输入ABCD
迭代
输入:BCD sofar:A…递归继续
输入:ACD sofar:B
输入:ABD sofar:C
输入:ABC sofar:D
希望这有帮助
if (input.equals("")) {
System.out.println(count + " " + sofar);
count++;
}
此步骤是在输入不是“
时传递的。请注意,您可以在此处简单地使用input.empty()
。此处需要记住的唯一一点是count
没有增加
检查下一个字符是否与当前字符相等,如果相等,则将使用
continue
关键字直接跳转到下一个迭代
(下一个字符)
如果没有,那么它将调用该方法(我们称之为
递归性
),传入字符串而不使用当前的字符
,但将其返回给sofar
permutation(input.substring(0, i) + input.substring(i + 1), sofar+c);
现在在输入为空的情况下
将打印非独立
字符的计数
以及所有这些字符
请记住,递归通常是一种停止条件,并且在假设递归有效的情况下,尝试使用递归调用解决较小的问题
我已经对代码进行了注释,因此您可以将其替换为您的副本,以便在返回代码时跟踪代码正在执行的操作。理解后添加您自己的注释,因为它们将帮助您了解发生的情况:
第1部分:基本条件/停止条件:
if (input.equals("")) {
System.out.println(count + " " + sofar);
count++;
}
这一部分停止递归并返回一个结果,基本情况是一个空字符串,它有一个同样是空字符串的排列
第2部分:迭代较小的问题
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
// ...
permutation(input.substring(0, i) + input.substring(i + 1), sofar+c);
}
让我们弄清楚这一点。它的意思是:“尝试并找到与所选字符相同的字符,如果存在,跳过此迭代并生成解决方案”
想想像“ABBA”这样的词,它会跳过第一个“a”和“B”,但不会跳过最后一个。为什么?因为相似字符的顺序并不重要(如果我们标记字符A1
B1
B2
A2
,现在替换它们:A2
B2
B1
A1
,这仍然是同一个单词,因此像“AA”这样的单词只有一个排列,因为A1
A2
与A2
A1
)
使用最后一个字符更容易,因为我们不需要维护本次迭代中已经使用的字符列表
包含基本注释的完整代码:
private static void permutation(String input, String sofar) {
if (input.equals("")) {
// this is the stop condition
// the only permutation of "" is ""
System.out.println(count + " " + sofar);
// this counts how many permutations were outputted.
count++;
}
for (int i = 0; i < input.length(); i++) {
// this loop basically means "take every
// possible character, and append permutations of all
// other characters to it.
char c = input.charAt(i);
if (input.indexOf(c, i + 1) != -1)
// this makes sure we only take a single "A" or "B"
// character in words like "ABBA", since selecting
// the same character would create duplicates
continue;
// this creates a new string without the selected character
// and under the assumption the recursion works
// appends all permutations of all other characters
// to it
permutation(input.substring(0, i) + input.substring(i + 1), sofar+c);
}
}
私有静态无效置换(字符串输入,字符串sofar){
if(输入。等于(“”){
//这是停止条件
//“”的唯一排列是“”
System.out.println(计数+“”+sofar);
//这计算输出了多少个排列。
计数++;
}
对于(int i=0;i
考虑使用调试器并单步执行代码。尝试过。但仍然没有意义。递归调用让我发疯。因此,我希望有人能用几行话来概括一下这里正在做什么。那将是非常好的。重新编写它是迭代的。任何递归算法都可以迭代实现可能的dup这正是我所缺少的。愚蠢是最好的。
permutation(input.substring(0, i) + input.substring(i + 1), sofar+c);
if (input.equals("")) {
System.out.println(count + " " + sofar);
count++;
}
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
// ...
permutation(input.substring(0, i) + input.substring(i + 1), sofar+c);
}
if (input.indexOf(c, i + 1) != -1)
continue;
private static void permutation(String input, String sofar) {
if (input.equals("")) {
// this is the stop condition
// the only permutation of "" is ""
System.out.println(count + " " + sofar);
// this counts how many permutations were outputted.
count++;
}
for (int i = 0; i < input.length(); i++) {
// this loop basically means "take every
// possible character, and append permutations of all
// other characters to it.
char c = input.charAt(i);
if (input.indexOf(c, i + 1) != -1)
// this makes sure we only take a single "A" or "B"
// character in words like "ABBA", since selecting
// the same character would create duplicates
continue;
// this creates a new string without the selected character
// and under the assumption the recursion works
// appends all permutations of all other characters
// to it
permutation(input.substring(0, i) + input.substring(i + 1), sofar+c);
}
}