2012-02-05 84 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