简体   繁体   English

方法的递归实现

[英]Recursive implementation of a method

I'm new to Java and still trying to wrap my head around recursion.The function below returns true at the very first intersection between the two sorted lists list x and list y. 我是Java新手,仍然想绕过递归。下面的函数在两个已排序列表list x和list y的第一个交点处返回true。

public static boolean checkIntersection(List<Integer> x, List<Integer> y) {
    int i = 0;
    int j = 0;
    while (i < x.size() && j < y.size()) {
        if (x.get(i).equals(y.get(j))) {
            return true;
        } else if (x.get(i) < y.get(j)) {
            i++;
        } else {
            j++;
        }
    }
    return false;
}

Now I've been trying to implement it using recursion instead, and I know that there should be a base case which is an empty list in this case and then try to reduce the list by excluding one element at a time and feed it back to the same recursive function, but I can't work out how to check for intersection as I pass the rest of the list over and over. 现在,我一直在尝试使用递归来实现它,并且我知道应该有一个基本情况,在这种情况下它是一个空列表,然后尝试通过一次排除一个元素来缩小列表并将其反馈给相同的递归函数,但是当我一遍又一遍地传递列表的其余部分时,我不知道如何检查交集。

public static boolean recursiveChecking(List<Integer> x,List<Integer> y) {
    if(x.size() == 0){
        return false;
    }
    else {
        return recursiveChecking(x.subList(1, x.size()-1), y);
    }
}

Any help would be highly appreciated. 任何帮助将不胜感激。 Thank you. 谢谢。

General approach to making something recursive is to think of two things: 进行递归操作的一般方法是考虑两件事:

  • When can I produce an answer trivially? 我什么时候可以简单地给出答案? - An answer to this question lets you code the base case. -这个问题的答案可让您编码基本情况。 In your situation, you can produce the answer trivially when at least one of two lists is empty (the result would be false ) or the initial elements of both non-empty lists are the same (the result would be true ) 在您的情况下,当两个列表中的至少一个为空(结果为false )或两个非空列表的初始元素相同(结果为true )时,您可以轻松得出答案。
  • How do I reduce the problem when the answer is non-trivial? 当答案不平凡时,如何减少问题? - An answer to this question lets you decide how to make your recursive call. -该问题的答案使您可以决定如何进行递归调用。 In your case you could, for example, remove the initial element of one of the lists before making the recursive call * , or pass ListIterator<Integer> in place of List<Integer> for a non-destructive solution. 例如,在您的情况下,您可以在进行递归调用*之前删除列表之一的初始元素,或者通过ListIterator<Integer>代替List<Integer>以获得无损解决方案。

* Of course in this case you need to take care of either adding your numbers back after the call, or make a copy of two lists before starting the recursive chain. *当然,在这种情况下,您需要注意在通话后加回您的号码,或者在启动递归链之前制作两个列表的副本。

As the lists are ordered, your recursion should remove the first element of the list with the smaller first value. 随着列表的排序,您的递归应删除列表中的第一个元素,其第一个值较小。 Then you have to return true, if both lists start with the same number and false if any of the lists is empty. 然后,如果两个列表都以相同的数字开头,则必须返回true;如果两个列表中的任何一个为空,则必须返回false。 Otherwise you keep removing elements. 否则,您将继续删除元素。 This would look something like this (This code is untested): 看起来像这样(未经测试的代码):

public static boolean recursiveChecking(List<Integer> x,List<Integer> y) {
    if(x.size() == 0 || y.size() == 0){
        return false;
    } else if (x.get(0).equals(y.get(0))) {
        return true;
    } else {
        if (x.get(0) < y.get(0)) {
            return recursiveChecking(x.subList(1, x.size()-1), y);
        } else {
            return recursiveChecking(x, y.subList(1, y.size()-1));
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM