【问题标题】:How should I write this Java function recursively?我应该如何递归地编写这个 Java 函数?
【发布时间】:2017-08-19 05:46:15
【问题描述】:

我正在尝试递归编写下面的 Java 代码:

public static int count(String word) {
    int numValue = 0;
    int length = word.length();
    for(int i=0; i<length; i++){
        String alpha = "abcdefghijklmnopqrstuvwxyz";
        String letter = (word.substring(0 + i,1 + i));
        numValue = numValue + alpha.indexOf(letter) + 1;
    }
    return numValue;
    }


public static void main(String[] args) {
    System.out.println(count("abc"));
}

函数返回输入字符串参数中每个字母的索引之和。

我正在尝试以递归方式编写相同的代码,谁能指出我哪里出错了?

public static int count(int numValue, int i, String word) {
    String alpha = "abcdefghijklmnopqrstuvwxyz";
    if( i >= word.length()){
        return numValue;
    }
    else{
        String letter = (word.substring(0 + i,1 + i));
        numValue = numValue + alpha.indexOf(letter) + 1;
        count(numValue, i=i+1, word);
    }
    return numValue;
}

  public static void main(String[] args) {
        System.out.println(count(0,0, "abc"));
    }

【问题讨论】:

  • 如果可以迭代,为什么还要递归呢?通常使用循环比所有这些递归调用更快......
  • 你的递归函数永远不会存储或使用递归调用的值做任何事情
  • 欢迎来到 Stack Overflow!寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定问题或错误和重现它所需的最短代码在问题本身。没有明确问题陈述的问题对其他读者没有用处。请参阅:How to create a Minimal, Complete, and Verifiable example。

标签: java loops recursion


【解决方案1】:

更简单的方法是一次又一次地缩小字符串。

public static int count(String word) {
    if (word.isEmpty()) {
        return 0;
    }
    final String alpha = "abcdefghijklmnopqrstuvwxyz";
    return alpha.indexOf(word.charAt(0)) + count(word.substring(1));
}

【讨论】:

    【解决方案2】:

    变量numValue 的类型为int。所以它不会在调用方法中改变,你将它传递给另一个方法并在那里改变它。这意味着,count(numValue, i=i+1, word); 行基本上什么都不做。

    您可以通过返回递归调用的结果来更正方法(由于您在if中返回,因此不需要else):

    public static int count(int numValue, int i, String word) {
        String alpha = "abcdefghijklmnopqrstuvwxyz";
        if( i >= word.length()){
            return numValue;
        }
        String letter = (word.substring(0 + i,1 + i));
        numValue = numValue + alpha.indexOf(letter) + 1;
        return count(numValue, i=i+1, word);
    }
    

    这就是为什么您的功能无法正常工作的原因。 timrau 提供的答案是递归的更好解决方案。

    【讨论】:

      【解决方案3】:

      要将此函数更改为递归,您最好使用相当容易实现的分而治之的方法。我会以简单详细的方式呈现出来,让大家看清楚步骤。

      public static int count(String word) {
          int numValue = 0;
          int length = word.length();
          if(length > 1)
          {
              String substring1 = word.substring(0, length/2);
              String substring2 = word.subtring(length/2 + 1, length);
              numValue += count(substring1);
              numValue += count(substring2);
              return numValue;
          }
          else 
          {
              String alpha = "abcdefghijklmnopqrstuvwxyz";
              return alpha.indexOf(word) + 1;
          }
      }
      
      public static void main(String[] args) {
          System.out.println(count("abc"));
      }
      

      我想您这样做是为了自学递归性 - 如果不是,您可能想考虑仅使用迭代方法而不是递归性对您来说是否更好。

      【讨论】:

      • 这不会编译,因为“字母”突然出现在你的 else 子句中。
      • 是的,你是对的,我打错了。我编辑了它-感谢您指出:)
      猜你喜欢
      • 2012-07-01
      • 2021-11-16
      • 2016-08-13
      • 1970-01-01
      • 2019-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多