计算整数数组中位于不同位置的公共元素的数量

Counting the number of common elements in integer arrays located at different positions

对于我的作业,我需要编写一个方法来 returns 在 2 个数组之间找到的奶牛数量(见下面的定义)。如果输入数组具有不同数量的元素,则该方法应抛出带有适当消息的 IllegalArgumentException。

公牛是在同一位置找到的整数数组中的公共数字,而牛是在不同位置找到的整数数组中的公共数字。请注意,如果一个数字已经是牛,则不能将其视为牛。

例如,考虑以下数组:

int[] secret = {2, 0, 6, 9};
int[] guessOne = {9, 5, 6, 2};
int[] guessTwo = {2, 0, 6, 2};
int[] guessThree = {1, 2, 3, 4, 5, 6};
int[] guessFour = {1, 3, 4, 4, 0, 5};

1) getNumOfCows(secret, guessOne) returns 2
2) getNumOfCows(secret, guessTwo) returns 0
3) getNumOfCows(secret, guessThree) returns an exception
4) getNumOfCows(guessThree, guessFour) returns 2

我在下面看到的方法非常适合示例 1 和 3,但是示例 2 和 4 存在问题,例如 getNumOfCows(secret, guessTwo) returns 1 而不是 0,因为 secret[ 0] 和 guessTwo[3] 被认为是一头牛。有人可以帮我修复我的代码吗?

// A method that gets the number of cows in a guess --- TO BE FIXED

  public static int getNumOfCows(int[] secretNumber, int[] guessedNumber) {

    // Initialize and declare a variable that acts as a counter

    int numberOfCows = 0;

    // Initialize and declare an array

    int[] verified = new int[secretNumber.length];

    if (guessedNumber.length == secretNumber.length) {

      // Loop through all the elements of both arrays to see if there is any matching digit

      for (int i = 0; i < guessedNumber.length; i++) {

        // Check if the digits represent a bull

        if (guessedNumber[i] == secretNumber[i]) {

          verified[i] = 1;
        }
      }

      for (int i = 0; i < guessedNumber.length; i++) {

        // Continue to the next iteration if the digits represent a bull

        if (verified[i] == 1) {

          continue;
        }

        else {

          for (int j = 0; j < secretNumber.length; j++) {

            if (guessedNumber[i] == secretNumber[j] && i != j) {

              // Update the variable

              numberOfCows++;

              verified[i] = 1;

            }
          }
        }
      }
    }

    else {

      // Throw an IllegalArgumentException

      throw new IllegalArgumentException ("Both array must contain the same number of elements");
    }

    return numberOfCows;
  }

首先检查并使用单独的数组标记所有公牛,以确保公牛的位置也被计为母牛

public static int getNumOfCows(int[] secretNumber, int[] guessedNumber) {
    int max = secretNumber.length;
    int cows = 0;
    int[] checked = new int[max];
    for (int i = 0; i < max; i++) {
        if (secretNumber[i] == guessedNumber[i]) {
          checked[i] = 1;
        }
    }

    for (int i = 0; i < max; i++) {
      if (checked[i] == 1) {
        continue;
      }
      for (int j = 0; j < max; j++) {
        if (secretNumber[i] == guessedNumber[j]) {
          cows++;
          checked[i] = 1;
        }
      }
    }
    return cows;
}

既然这个答案被接受了,original question 可以投票关闭作为重复项

我在这里发布我对一个重复问题的回答,如果这个问题获得批准,那么另一个问题可以作为重复问题关闭。

问题是一个元素在至少一个数组中出现多次将不会被正确处理。

一个可能的解决方案可能是这个:

  1. 创建牛名单。
  2. 遍历两个数组并添加 每个 两个数组中尚未添加的元素。 (注意:复杂度为 n²)
  3. 现在所有 可能的奶牛 都在列表中,遍历具有相同索引的数组位置,如果找到公牛,则从奶牛列表中删除编号。
  4. 现在奶牛列表只包含奶牛。

此解决方案可能比您当前的解决方案慢一点,但我认为它工作正常。