简体   繁体   English

如果 integer 在数组中仅存在一次,则返回 true 的递归方法

[英]Recursive method that returns true if the integer exists only once in an array

For example:例如:

int[] arr = {1,2,2,3,4,5};

3 exists only once the method should return true , 2 exists twice the method should return false , 0 doesn't exist the method should return false . 3只存在一次该方法应该返回true2存在两次该方法应该返回false0不存在该方法应该返回false I keep getting a runtime error, how do I fix it or is there any way to the method (recursions only).我不断收到运行时错误,我该如何修复它或者该方法有什么方法(仅限递归)。 Here is my code:这是我的代码:

public static boolean list(int[] a, int num) {
    return helper(a, 0, num);
}

public static boolean helper(int[] a, int i, int num) {
    int count = 0;
    if (a[i] == num)
        count++;
    if (count == 1 && i == a.length)
        return true;
    else if (count != 1 && i == a.length)
        return false;
    else
        return helper(a, i++, num);
}

I ran your code and got a java.lang.StackOverflowError .我运行了您的代码并得到了java.lang.StackOverflowError That indicates that your recursive method, named helper keeps calling itself infinitely.这表明您的递归方法,名为helper会无限地调用自己。 It does that because neither of the conditions in the if statements that precede the return statements are ever true.这样做是因为在return语句之前的if语句中的条件都不是真的。

You don't want to recursively call the method when one of the following two conditions are true:当以下两个条件之一为真时,您不希望递归调用该方法:

  1. You have encountered a second occurrence of the number you are searching for in the array.您在数组中遇到了要搜索的数字的第二次出现。
  2. You have reached the end of the array.您已到达数组的末尾。

If you keep initializing count to zero in every call to method helper , it will never be greater than one.如果在每次调用方法helper时都将count初始化为零,则它永远不会大于一。 Hence you should make it a parameter of method helper .因此,您应该将其设为方法helper的参数。

You just need to check whether i equals a.length to determine whether you have reached the end of the array.您只需要检查i是否等于a.length以确定您是否已到达数组的末尾。

/**
 * Determines whether 'num' occurs exactly once in 'a'.
 *
 * @param a     - array to search
 * @param count - number of occurrences of 'num' in 'a'
 * @param i     - index in 'a'
 * @param num   - number to search for
 *
 * @return 'true' if 'num' occurs exactly once in 'a', otherwise 'false'.
 */
public static boolean helper2(int[] a, int count, int i, int num) {
    if (count > 1) {
        return false;
    }
    if (i == a.length) {
        return count == 1;
    }
    if (a[i] == num) {
        count++;
    }
    return helper2(a, count, i + 1, num);
}

And the initial call to the method is (for example, if you are checking whether the number 3 appears only once in the array.并且对该方法的初始调用是(例如,如果您正在检查数字3是否仅在数组中出现一次。

int[] a = new int[]{1, 2, 2, 3, 4, 5}; // array of integers to search
int count = 0; // number of occurrences
int index = 0; // index in 'a'
int num = 3; // number to search for
boolean single = helper2(a, count, index, num);

This is much easier to do with an iterative method :使用迭代方法更容易做到这一点:

public static boolean iterativeMethod(int[] arr, int number) {
    // filter the desired numbers in the array
    // and check if their quantity is equal to '1'
    return Arrays.stream(arr).filter(i -> i == number).count() == 1;
}
public static void main(String[] args) {
    int[] arr = {1, 2, 2, 3, 4, 5};
    System.out.println(iterativeMethod(arr, 3)); // true
    System.out.println(iterativeMethod(arr, 2)); // false
    System.out.println(iterativeMethod(arr, 0)); // false
}

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

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