【问题标题】:Finding the 'maximum' character in a linked list Java recursively递归查找链表Java中的“最大”字符
【发布时间】:2020-06-06 21:07:06
【问题描述】:

我正在尝试使用辅助函数递归地在链表中查找“最大值”。我刚开始在课堂上学习这些,我很困惑。我们有一个自定义类,它定义了 Node 类型和另一个函数来计算 Node 或链表的大小。我在比较整数时解决了这个问题,但是我迷路了。这是我的代码: '''

    static class Node {
        public Node (char item, Node next) { this.item = item; this.next = next; }
        public char item;
        public Node next;
    }

    Node first;   // this is the only instance variable,
                  // the access point to the list

    // size
    //
    // a function to compute the size of the list, using a loop
    //  an empty list has size 0
    public int size () {
        int count = 0;
        for (Node tmp = first; tmp != null; tmp = tmp.next)
            count++;
        return count; 
    }


    /*
     * maxCharacter
     * 
     * a function to compute the 'maximum' character in the list using recursion
     * You will want to create a helper function to
     * do the recursion
     * 
     * precondition: list is not empty 
     * 
     * Examples: 
     * ["ababcdefb"].maxCharacter() == 'f'
     * ["eezzg"].maxCharacter() == 'z'
     * ["a"].maxCharacter() == 'a'
     */
    public char maxCharacter () {

        return maxCharacterHelper(first, first.size());

    }

    public char maxCharacterHelper(Node first, int index) {
        char[] alpha = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
        int max = 0;
        while(index > 0 )
            max = alpha.indexOf(first.item) > max ? first.item : max;
            maxCharacterHelper(first, index-1);
        return max;
    }

''' 如果您能解释我如何在保持最大字符的同时递归循环遍历列表,我将不胜感激。

【问题讨论】:

  • 我不明白你的while循环,你从来没有减少索引的大小你只需要检查node.next是否为null

标签: java recursion linked-list nodes


【解决方案1】:

您的 while 循环令人困惑,因为缩进并且您从不更改索引。但是,如果您的意图是使用递归,我认为您不需要它。通常,使用递归,您需要建立一个不能递归的基本案例。对于链表,自然的基本情况是没有下一个节点,而不是基于索引。

if (current.next == null)
    return alpha.indexOf(current.item);

否则将递归返回与当前值结合起来

int remainingMax = maxCharacterHelper(current);
int currentValue = alpha.indexOf(current.item);
return (remainingMax > currentValue) ? remainingMax : currentValue;

这就是我的组合方式

//I made it static because it is not a method of a specific Node
public static int maxCharacterHelper(Node currentNode){
   // remaining list includes only current node, so this one has max value
   if (current.next == null)
       return alpha.indexOf(current.item);
   //otherwise take the larger of remaining list and current node
   int remainingMax = maxCharacterHelper(current.next);
   int currentValue = alpha.indexOf(current.item);
   return (remainingMax > currentValue) ? remainingMax : currentValue;
}

【讨论】:

    【解决方案2】:

    递归的黄金法则是“先考虑基本情况,然后编写递归”。

    在这种情况下,基础是空列表。在这种情况下,最大值是您看到的最后一个值。

    重复循环只是对列表中具有您调用的最高值的其余部分的调用。

    public static MaxNode(Node n, char currentMax) {
      if (n == null) // base case, we're at the end.
        return currentMax;
    
      // recurrence
      return MaxNode(n.next, currentMax > n.item ? currentMax : n.item);
    }
    

    对于简单的 ASCII 值,您可以使用 > 运算符处理最大值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-17
      相关资源
      最近更新 更多