2012-02-05 82 views
2

這裏保持計數就是我想用這個程序來完成:來檢查,如果一個子實例的數量相匹配實例指定金額,返回boolean值的遞歸方法。在遞歸Java方法

這是我對這個特定的遞歸方法的問題:我想能夠移動遞歸方法體內的計數器,但是,我遇到了計數器在每次遞歸調用重置時的問題它在方法體中。我已經能夠使它發揮作用的唯一途徑是通過使用函數體的外部聲明的靜態計數器變量。是否有任何其他的技術,我可以馬歇爾爲了能夠在宅院方法體,使這種方法可以作爲一個「黑盒子」行動的方法的櫃檯?

感謝您的任何意見或見解可以提供。

public class strCopies { 

    //count instances of part and whole equality 
    static int count = 0; 

    public static boolean copies(String whole, String part, int check) 
    { 

     //check if current string length is valid 
     if(whole.length() < part.length()) 
     { 
      //check if check parameter equals part instances 
      if(count == check) 
      { 
       return true; 
      } 
      else 
      { 
       return false; 
      } 
     } 

     //check if current string value is an instance of part 
     if(whole.substring(0, 3).equals(part)) 
     { 
      count++; 
     } 

     //recursive call 
     return copies(whole.substring(1), part, check); 

    } 

    public static void main(String[] args) 
    { 
     System.out.println(copies("dogcatdog", "cat", 2)); 
    } 
} 

回答

1

你就要成功了:你應該check變量的含義改變爲比賽的剩餘數量,而不是原號碼請。然後,你可以重寫方法沒有保留額外的數可言,如下:

public static boolean copies(String whole, String part, int check) 
{ 

    //check if current string length is valid 
    if(whole.length() < part.length()) 
    { 
     //check if check parameter equals part instances 
     if(check == 0) 
     { 
      return true; 
     } 
     else 
     { 
      return false; 
     } 
    } 

    //check if current string value is an instance of part 
    if(whole.substring(0, 3).equals(part)) 
    { 
     check--; 
    } 
    return return copies(whole.substring(1), part, check); 
} 
+0

謝謝,我沒有想到這種方法,但它顯然是所有提出的解決方案中最優雅的。 – gryb 2012-02-05 19:37:30

+0

@gryb不客氣!減少剩餘步數的方法在遞歸解決方案中很常見,這可能是因爲遞歸的「結束條件」看起來更自然一些。 – dasblinkenlight 2012-02-05 20:07:08

0

簡單的版本:

創建包含計數器的類。 在你的main上初始化它。 傳遞其參考作用。

另一個想法:

創建一個靜態計數器和你的函數X. 內其構造一個單獨的類添加一個到它的櫃檯和調用函數X.

然後,而不是運行你的函數喜歡你之前做過,「創造」那個階級,從而增加櫃檯和調用功能。

整潔的事情是,你可以繼承這個類,並重新定義X到你在後一階段選擇的任何東西,所以你可以得到這個通用類來計算每個函數的激活。

0
public int countRecursive(String whole, String part){ 
    if(whole.length() < part.length()) return 0; 
    if(part.length()==0) return -1; // ints can't express "Infinity" 
    // maybe you want to return -1 only if whole is not null, and something else if it is.  

    int count = 0; 

    if(whole.substring(0, part.length()).equals(part)) 
     count = 1; 
    return countRecursive(whole.substring(1), part) + count; 
} 

public boolean count(String whole, String part, int check){ 
    return countRecursive(whole, part) == check; 
} 

請注意,這會消除計數器的代價,從而爲每個狀態創建一大串字符串。 (你有給每個字符串的長度代替單個int)。但話又說回來,如果你想表現,那麼你不應該使用遞歸這樣的事情來。一個簡單的for循環會做得更好。

0

你可以在櫃檯添加到方法的參數如下:

public class strCopies { 

    public static boolean copies(String whole, String pargs, int check){ 
     return copies(whole, pargs, check, 0); 
    } 

    public static boolean copies(String whole, String part, int check, int count) 
    { 

     //check if current string length is valid 
     if(whole.length() < part.length()) { 
      //check if check parameter equals part instances 
      if(count == check) { 
       return true; 
      } 
      else { 
       return false; 
      } 
     } 

     //check if current string value is an instance of part 
     if(whole.substring(0, 3).equals(part)) { 
      count++; 
     } 

     //recursive call 
     return copies(whole.substring(1), part, check, count); 

    } 

    public static void main(String[] args) { 
     System.out.println(copies("dogcatdog", "dog", 2)); 
    } 
} 
0

您可以通過計數作爲參數傳遞給遞歸函數,這樣就不會被「復位」時,該方法被稱爲。

public static boolean copies(String whole, String part, int check, int count) 
{ 

    //check if current string length is valid 
    if(whole.length() < part.length()) 
    { 
     //check if check parameter equals part instances 
     if(count == check) 
     { 
      return true; 
     } 
     else 
     { 
      return false; 
     } 
    } 

    //check if current string value is an instance of part 
    if(whole.substring(0, 3).equals(part)) 
    { 
     count++; 
    } 

    //recursive call 
    return copies(whole.substring(1), part, check, count); 

} 
0

不知道什麼是你的遞歸方法做。但是,要維護一個計數器,可以將它作爲參數傳遞給遞歸方法。

public boolean copies(String whole, String part, int check, int count) { 

    // your code here.... 

    if(whole.substring(0, 3).equals(part)) 
    { 
     count++; 
    } 

    //recursive call 
    return copies(whole.substring(1), part, check, count); 
} 

當你對copies方法第一個電話,你需要通過0到您的count