【问题标题】:Keeping count in a recursive Java method在递归 Java 方法中保持计数
【发布时间】:2012-02-05 04:17:38
【问题描述】:

这是我试图用这个程序完成的:一个递归方法,它检查子字符串的实例数是否与指定的实例数匹配,返回一个布尔值。

这是我在使用这种特定递归方法时遇到的问题:我希望能够在递归方法主体内移动计数器,但是,我遇到了计数器在每次递归调用时重置的问题在方法体中。我能够使其工作的唯一方法是使用在函数体外部声明的静态计数器变量。是否有任何其他技术我可以编组以便能够将方法的计数器定位在方法主体中,以便该方法可以充当“黑匣子”?

感谢您提供的任何建议或见解。

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));
    }
}

【问题讨论】:

    标签: java methods recursion


    【解决方案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);
    }
    

    【讨论】:

    • 谢谢,我没有想到这种方法,但它显然是所有提议的解决方案中最优雅的。
    • @gryb 不客气!减少剩余步数的方法在递归解决方案中非常常见,可能是因为递归的“结束条件”看起来更自然一些。
    【解决方案2】:

    您可以将计数作为参数传递给递归函数,这样在调用方法时它不会“重置”。

    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);
    
    }
    

    【讨论】:

      【解决方案3】:
      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 循环会做得更好。

      【讨论】:

        【解决方案4】:

        您可以将计数器添加到方法参数中,如下所示:

        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));
            }
        }
        

        【讨论】:

          【解决方案5】:

          简单版:

          创建一个包含计数器的类。 在你的主服务器上初始化它。 将其引用传递给函数。

          另一个想法:

          使用静态计数器和函数 X 创建一个单例类。 在其构造函数中,将其计数器加一并调用函数 X。

          然后,不要像以前那样运行你的函数,而是“创建”那个类,从而增加计数器并调用函数。

          巧妙的是,您可以继承该类并将 X 重新定义为您在后期选择的任何内容,这样您就可以得到这个依赖于每次激活函数的通用类。

          【讨论】:

            【解决方案6】:

            不确定您的递归方法在做什么。但是,要维护一个计数器,您可以将它作为参数传递给您的递归方法。

            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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2017-06-24
              • 1970-01-01
              • 2016-02-27
              • 1970-01-01
              • 1970-01-01
              • 2010-09-07
              相关资源
              最近更新 更多