2017-04-17 3 views
-3

私は再帰問題を練習していました。ループなしでStringにある特定の文字の数を数えたいと思う質問があります。文字列内の特定の文字の数を再帰的に数えるにはどうすればよいですか?

どのような方法を使用しますか?誰も私に説明することができますか?

ここでは、「文字列を指定すると、文字列の小文字の「x」文字の数を再帰的に(ループなしで)計算します。

私のコード:

public int countX(String str) { 
    if(str.length()==0){ 
     return 0; 
    } 
    if(str.charAt(0)=='x'){ 
     return 1+ countX(str.substring(1)); 
    } 
    return countX(str); 
} 
+0

ヘイ・アレン、[尋ねる]をチェックしてください - より具体的な質問の横にいくつかのコードを表示する必要があります。 – domsson

+0

あなたはこれまでに何を試しましたか? –

+1

コードに問題はありますか?何がうまくいかない?何かエラーが出ますか? – BackSlash

答えて

3

あなたはほとんどそこにいる、しかし、あなたはstrの最初の文字が'x'でない場合を修正する必要があります。 str.charAt(0) != 'x'の場合、再帰的にcountXstrにコールするので、現在行っている処理は無限再帰を引き起こします。しかし、strは、その最初の文字として'x'を持っていなかったものと同じ文字列がまだあるので、それは、何度も何度も何度もstr上などをcountXを呼び出すために起こっている...だから、唯一str.substring(1)その最初の文字の上にcountXを呼び出すためのソリューションですそのような'x'ないが、

public int countX(String str) { 
    if(str.length()==0){ 
     return 0; 
    } 
    if(str.charAt(0)=='x'){ 
     return 1 + countX(str.substring(1)); 
    } 
    else{ 
     return countX(str.substring(1)); 
    } 
} 
あなたの方法をやっていた何を

は、ここでは、コールスタックトレースは次のようになります方法です

、のは、私が"Hello"countXと呼ばれるとしましょう新しいソリューションは、溶液中で、我々は常にベースケース(str.length()==0)に取得する方法を持っているか

call countX("Hello") 
"Hello".length() != 0 so we move on to the next condition 
"Hello".charAt(0) != 'x' so we call countX("ello") 
    call countX("ello") 
    "ello".length() != 0 so we move on to the next condition 
    "ello".charAt(0) != 'x' so we call countX("llo") 
     call countX("llo") 
     "llo".length() != 0 so we move on to the next condition 
     "llo".charAt(0) != 'x' so we call countX("lo"); 
      call countX("lo") 
      "lo".length() != 0 so we move on to the next condition 
      "lo".charAt(0) != 'x' so we call countX("o"); 
       call countX("o") 
       "o".length() != 0 so we move on to the next condition 
       "o".charAt(0) != 'x' so we call countX(""); 
        call countX("") 
        "".length() == 0 so return 0 
       return 0 
      return 0 
     return 0 
    return 0 
return 0 

お知らせを何

call countX("Hello") 
"Hello".length() != 0 so we move on to the next condition 
"Hello".charAt(0) != 'x' so we call countX("Hello") 
    call countX("Hello") 
    "Hello".length() != 0 so we move on to the next condition 
    "Hello".charAt(0) != 'x' so we call countX("Hello") 
     call countX("Hello") 
     "Hello".length() != 0 so we move on to the next condition 
     "Hello".charAt(0) != 'x' so we call countX("Hello"); 
      . 
      . 
      . 
      infinite recursion 

。以前は、メソッドが基底ケースに達することを妨げるインスタンス(xではない文字に遭遇したとき)が存在するのに対し、

+0

ありがとう、最初の 'char'が' x'でなければ、メソッド自体を '返す 'ことを忘れました。 –

2

どのような方法を使用しますか?誰も私に説明することができますか?

一つの解決策は、StringBuilderを使用し、単にメソッドの個々の呼び出しで、最初の文字を削除することで、最終的に、あなたは基本ケースを取得し、出力する必要があり、正しい結果でしょう。

public int countX(String str) { 
     if(str.length() == 0) return 0; 

     StringBuilder builder = new StringBuilder(str); 
     if(str.charAt(0) == 'x'){ 
      builder.deleteCharAt(0); 
      return 1 + counter(builder.toString()); 
     } 

     builder.deleteCharAt(0); 
     return counter(builder.toString()); 
} 

、あなたの現在のソリューションを続行したい場合は、あなたがこの置き換える必要があります:これで

return countX(str); 

を:

return countX(str.substring(1)); 

問題があればということでした現在の文字は'x'ではありません。単純に無視して、問題を基底ケースに単純化しませんでした。

0

私はあなたの必要性を理解しています。以下では、文字列内の文字を再帰的に数えるために必要なメソッドがあります。

static int countCharRecursively(String str, char ch){ 

    if(str.indexOf(ch) != -1) 
    {   
     return countCharRecursively(str.substring(0, str.indexOf(ch)), ch)+ 
      countCharRecursively(str.substring(str.indexOf(ch)+1),ch) + 1; 
    } 
    else{ 
     return 0; 
    } 
} 

実行可能な完全なプログラムです。

public class CountCharRecursivelyInString{ 

public static void main(String[] args){ 

    String str = "hello the world"; 

    char ch = 'l'; 

    int nbr = countCharRecursively(str, ch); 

    System.out.println(ch + " : " + nbr); 
} 

static int countCharRecursively(String str, char ch){ 

    if(str.indexOf(ch) != -1) 
    {   
     return countCharRecursively(str.substring(0, str.indexOf(ch)), ch)+ 
      countCharRecursively(str.substring(str.indexOf(ch)+1),ch) + 1; 
    } 
    else{ 
     return 0; 
    } 
} 
} 
関連する問題