Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 递归-算法的实现_Java_Algorithm - Fatal编程技术网

Java 递归-算法的实现

Java 递归-算法的实现,java,algorithm,Java,Algorithm,我试图理解用于查找字符串排列的算法 如果你已经过了最后一个职位 - Print the string - Return 否则 - For each letter in the input string - If it’s marked as used, skip to the next letter - Else place the letter in the current position - Mark the letter as used - Permute remaining lett

我试图理解用于查找字符串排列的算法

如果你已经过了最后一个职位

- Print the string
- Return
否则

- For each letter in the input string
- If it’s marked as used, skip to the next letter
- Else place the letter in the current position
- Mark the letter as used
- Permute remaining letters starting at current position + 1
- Mark the letter as unused


下面是该算法的Java实现:

公共类置换{
使用私有布尔[];
私有StringBuilder out=新StringBuilder();
私人最终串入;
公共排列(最终字符串str){
in=str;
used=新布尔值[in.length()];
}
公共无效排列(){
如果(out.length()==in.length()){
System.out.println(out);
返回;
}
对于(int i=0;i


我可以理解代码的其他部分,除了注释为
这一行的那一行
。工作情况如何?

如果您询问“继续”
工作情况如何:

发件人:

continue语句跳过for、while或do while循环的当前迭代。未标记的表单跳到最内层循环体的末尾,并计算控制循环的布尔表达式

在您的程序中,您正在初始化
used=new boolean[In.length()]因此默认情况下,此数组中的所有值都将为false


if(使用[i])继续如果使用的
的当前值[i]
true
表示已处理,则它将跳过当前迭代并继续下一步。

突出显示的行的目的是, 代码正在检查位置“i”处的字符是否已被使用。如果已被使用,则(即直接转到for循环中的下一次迭代)

相当于

for( int i = 0; i < in.length(); ++i ){
      if(!used[i] ) {
          out.append( in.charAt(i) );
          used[i] = true;
          permute();
          used[i] = false;
          out.setLength( out.length() - 1 );
      }
}
for(int i=0;i
希望这有帮助


祝你好运

continue将跳过当前循环体,并从循环的起始点开始迭代,然后再次检查条件以再次进入循环。 在代码中

for( int i = 0; i < in.length(); ++i ){
   if( used[i] ) continue;   ------------------- // This line
   out.append( in.charAt(i) );
   used[i] = true;
   permute();
   used[i] = false;
   out.setLength( out.length() - 1 );
  }
for(int i=0;i

如果
used[i]==true
,那么下面的“继续”语句将不会对当前迭代执行。控件将转到循环语句中的条件语句。

它只是跳过if块下语句的执行,并继续下一个i值的迭代

@MavenMaverick接受任何答案,因此它会帮助其他寻求相同答案的人problem@AJ. 答案是完全正确的,实际上回答了这个问题,尽管有点问题。
for( int i = 0; i < in.length(); ++i ){
   if( used[i] ) continue;   ------------------- // This line
   out.append( in.charAt(i) );
   used[i] = true;
   permute();
   used[i] = false;
   out.setLength( out.length() - 1 );
  }