【问题标题】:Java recursion countJava 递归计数
【发布时间】:2011-11-14 15:40:29
【问题描述】:

首先,这不是家庭作业。只有我在练习。 我试图递归地确定“hi”出现在给定字符串中的次数,但在每种情况下,它都会跳到最后一个 else if 语句并且字符串为空。有什么想法吗?

基本上, if(字符串以“hi”开头) 将 count 加 1 并在第二个索引之后使用字符串递归以跳过它刚刚计数的“hi”

else if(字符串不以“hi”开头且字符串不为空) 递归第一个索引后的字符串,以查看下一次它是否以“hi”开头。

else if(字符串为空) Print("到达文本结尾") 返回计数;

public class Practice {

  public int recur(String str, int counter){
      int count=counter;
      if(str.startsWith("hi")){
          count++;
          recur(str.substring(2),count);
      }
      else if((!str.isEmpty())&&(!str.startsWith("hi"))){
          recur(str.substring(1),count);
      }
      else if(str.isEmpty()){
          System.out.println("End of text reached");
          return count;
      }
      return count;
  }

  public static void main(String args[]){
      String str="xxhixhixx";
      Practice p=new Practice();
      System.out.println(p.recur(str, 0));
  }
}

【问题讨论】:

  • 相比字符串处理wiki,在递归实践中确实有更好的例子。
  • 我对此有疑问 - 任何调用者都可以修改结果,因为 counter 是外部调用的一部分。至少,这应该是private,使用public 包装器 包含counter(并且不是递归的)。另一种方法是在递归返回期间进行加法。此外,您正在检查 startsWith,然后将光标移动 1 - indexOf 有什么问题(我认为那里有更好的优化)。

标签: java recursion


【解决方案1】:

这是练习调试递归函数调用的好机会——实际上非常困难。建议:

  • 使用策略性放置的打印语句来确保从一个递归调用到下一个递归调用正确更改参数
  • 重构 if 语句中的案例分析顺序,使其更加清晰。例如,1)检查字符串是否为空(基本情况),2)检查字符串是否以“hi”开头,3)catch-all -- 不为空且不以“hi”开头

【讨论】:

  • 关于策略性放置的打印语句,我喜欢将打印与递归的深度“对齐”,如下所示:stackoverflow.com/questions/7774769/…
  • @user988052 -- 这对于一些递归问题很方便,但对于涉及树的问题更是如此。这是一个“线性”递归问题,所以没有那么有用。
【解决方案2】:

正如@Steve 所说,您必须使用recur 返回的返回值。

请参阅下面的代码修改版本,我还简化了您的 if/else 语句:

public int recur(String str, int counter) {

  if (str.startsWith("hi")) {
    return recur(str.substring(2), counter+1);
  } else if (!str.isEmpty()) {
    return recur(str.substring(1), counter);
  } else {
    System.out.println("End of text reached");
    return counter;
  }
}

public static void main(String args[]) {
    String str = "xxhixhixx";
    Practice p = new Practice();
    System.out.println(p.recur(str, 0));
}

【讨论】:

  • 这是正确的解决方案。发生的事情是顶级函数调用遇到了最后一个 return 语句,该语句返回零,因为它没有对递归调用的返回值做任何事情。
  • 哦,我现在想起来了。谢谢你们的帮助;我已经有一段时间没有使用递归了,哈哈。
【解决方案3】:

您没有使用从 recur 返回的值。

【讨论】:

    【解决方案4】:
      public int countHi(String str) {
          if (str.length() <= 1) {
          return 0;
          }
          int count = 0;
          if (str.substring(0, 2).equals("hi")) {
          count = 1; 
          }
          return count + countHi(str.substring(1)); //substring off
        }
    

    所有这一切都是在一个更大的字符串中递归地计算字符串“hi”的数量。其余的实现应该是小菜一碟,快乐的编码!

    【讨论】:

      【解决方案5】:

      您的程序打印“文本结尾”是正确的,因为最终按照它将到达那里的逻辑,计数始终为 0 的原因是,在每次迭代中,它们都会更改自己的副本,最后在达到终止条件时( String 为空)结果从堆栈中弹出,因此您收到的最终结果是第一次迭代的弹出,其中 count 为 0,因此您必须在每一步返回 recur 返回的值,而不是返回 count。

      【讨论】:

        【解决方案6】:
        public static int recursive(String givenStr) {
        
            int count =0 ;
        
            Pattern pattern = Pattern.compile("hi");
            Matcher match = pattern.matcher(givenStr);
            while(match.find()){
                System.out.println(match);
                count++;
            }
            return count;
        }
        

        这将返回“hi”出现在字符串中的次数

        【讨论】:

          猜你喜欢
          • 2015-02-20
          • 2017-09-07
          • 1970-01-01
          • 2014-12-28
          • 2016-03-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-10-10
          相关资源
          最近更新 更多