【问题标题】:Returning multiple values from a recursive function从递归函数返回多个值
【发布时间】:2012-10-16 01:00:33
【问题描述】:

我有这个问题,我必须将十进制数转换为二进制,然后将这些位存储在一个链表中,其中头节点是最高有效位,最后一个节点是最低有效位。解决问题本身其实很简单,你只需要不断递归取模 2 并将结果添加到列表中,直到十进制数变为 0。

我卡住的地方是我必须编写函数,使其返回最高有效位和最后一个有效位的一对数字(无论是数组还是列表)。 即:在函数中输入 14 将返回 (1, 0),因为 14 是二进制的 1110。

我确实可以轻松访问 MSB 和 LSB(getFirst()、getLast())。

函数只能接受一个参数,即十进制数。

目前我有这个当前代码:

public static void encodeBin(int n) {
    if(n == 0) return; //Base case
    else {
        if(n % 2 == 0)
            theList.addFirst(0);
        else
            theList.addFirst(1);
        encodeBin(n / 2);
    }
    // return?
}

问题是我不知道如何返回这 2 个值。有返回值意味着我不能自己调用​​ encodeBin()。

此外,我应该在哪里创建列表?如果我在函数的最开头放置List<Integer> = new LinkedList<Integer>() 之类的内容,那么每次函数调用自身时,它都会创建一个新列表并在该新列表中添加位而不是原始列表中的位吗?(从函数创建时创建的列表第一次调用)

有人知道怎么解决吗?

【问题讨论】:

  • 您可以使用return theList.addFirst(encodeBin(<next_value));theList.addList(encodeBin(<next_value>));return theList; 或类似名称,具体取决于您的设计。

标签: java recursion binary linked-list return


【解决方案1】:

您不能返回 2 个值。您将不得不返回一些包含 2 个值的对象。一个数组或一些新对象,取决于你的作业要求和这个函数的使用位置。

对于链表的创建,你需要的是一个递归辅助方法。您的公共方法将用于初始化您的对象、开始递归并返回您的结果。这允许您的实际递归函数有多个参数。

public static SOME_TYPE encodeBin(int n) {
  LinkedList result = new LinkedList();
  encodeBin_helper(result,n);
  // return the MSB and LSB
}

public static void encodeBin_helper(LinkedList theList, int n) {
  if(n == 0) return; //Base case
  else {
    if(n % 2 == 0)
        theList.addFirst(0);
    else
        theList.addFirst(1);
    encodeBin_helper(theList, n/2);
  }

}

【讨论】:

    【解决方案2】:

    您不能分别返回两个值。但是,您可以返回一个包含第一位和最后一位的数组创建您自己的类来保存这些数据,并返回该类的一个实例。


    关于列表,我看到两个选项:

    1. 将其设为static 类变量
    2. 让它成为函数的参数(虽然我看到你说你不能这样做)。

    第一种方法如下所示:

    public class MyClass {
        private static List<Integer> theList = new LinkedList<Integer>();
    
        // `encodeBin` method as you have it
    }
    

    第二种方法如下所示:

    public static void encodeBin(int n, List<Integer> theList) {
        if(n == 0) return; //Base case
        else {
            if(n % 2 == 0)
                theList.addFirst(0);
            else
                theList.addFirst(1);
            encodeBin(n / 2, theList);
        }
    }
    

    然后你可以按照

    的方式做一些事情
    List<Integer> theList = new LinkedList<Integer>();
    encodeBin(14, theList);
    

    theList 将根据需要保存适当的位。


    作为说明,您可能需要考虑将其设为 booleans 而不是 整数 的列表,true 代表 1false 代表 @ 987654329@.

    【讨论】:

    • 仅供参考:发帖者说该函数只能接受 1 个参数。
    【解决方案3】:

    我建议声明两种方法:

    (1) public static int[] encodeBin(int n) 和 (2) private static void encodeBin(LinkedList, int n)

    公共方法只是创建一个空列表,然后调用私有版本,同时传递空列表和原始输入 n 作为参数

    类似这样的:

    public static int[] encodeBin(int n) {
      LinkedList<Integer> aList = new LinkedList<Integer>();
      encodeBin(aList , n);
      int MSB = aList.getFirst();
      int LSB = aList.getLast();
    
      return new int[] {MSB, LSB};
    }
    
    private static void encodeBin(LinkedList<Integer> list, n) {
      //your recursive version here
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-11
      • 1970-01-01
      • 1970-01-01
      • 2014-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多