教科書によると、作家はjavaの順列を実現するために特別な方法を使用するように頼んだ。 は、より正確に言えば、二つの機能が存在しますjava Permutation(再帰)の特別なソリューションですか?
public void static displayPermutation(String s)
を----単に作者の観点から、次の機能displayPermutation("", String s);
を起動している、我々は後にある「S」から1つの文字を移動する必要がありますコンマを最初の1つのステップにある文字列に繰り返します。
しかし、私はそれを実現するためにあらゆる努力をしていますが、私のコードは機能しません。 私のコードは次のとおりです。
public static void displayPermuation(String s){
displayPermuation("", s);
}
public static void displayPermuation(String s1, String s2){
if(s2.length() == 0){
System.out.println(s1);
return;
}else{
for(int i = 0; i < s2.length(); i++){
s1 = String.format(s1 + "%s", s2.charAt(i));
s2 = String.format("%s%s", s2.substring(0, i), s2.substring(i + 1, s2.length()));
displayPermuation(s1, s2);
}
}
}
「働いていない」とはどういう意味ですか?何が起こると予想され、実際の結果は何ですか? – Guy
forループの最初の繰り返しの後に、文字列 's1'と' s2'はもはやあなたが仮定している文字列ではありません。 – Henry