【问题标题】:Checks if two arrays have elements in the same order检查两个数组是否具有相同顺序的元素
【发布时间】:2014-11-06 00:14:53
【问题描述】:

如何检查两个数组(循环)是否具有相同顺序的相同元素。 例如,我们以数组 [1,2,3,4] 为例。

对于 [2,3,4,1]、[3,4,1,2]、[4,1,2,3] 的测试应该返回 true,但对于 [1,3,2,4] 不应该返回 true , [1,4,2,3] 或 [1,2,3,5]。

我最初的方法是找到第一个匹配项 - 每个数组中的一个元素相等 - 并将这两个元素视为它们各自数组的初始元素,我逐一比较了数组的其余元素。

有没有更好的方法? 谢谢。

【问题讨论】:

  • 是否允许重复?没有重复它相当微不足道,重复仍然微不足道,但不太明显。
  • 您的方法是一个好的开始,但对于[1,2,1,3] 和[1,3,1,2] 会失败。只需将第一个视为无休止的重复,然后在其中搜索第二个(仅限于索引 0 到 N)

标签: java arrays


【解决方案1】:

如果一个数组是循环的,那么array+array 拥有另一部分的整个数组。 例如:

 [2 3 4 1] append [2 3 4 1] = [2 3 4 1 2 3 4 1]
                                    |-------|

如您所见,[1 2 3 4] 在同一数组的两次附加中是“某处”。

因此,通过这种逻辑,您可以执行O(n*m) 操作来检查每个案例以查看它是否匹配(n 是 array1,m 是 array2):

 //array1 has [2 3 4 1 2 3 4 1]
 //array2 has [1 2 3 4]
 boolean check = false;
 for(int i = 0; i < array1.length(); i++) {
   for(int j; j < array2.length(); j++) {
      if((i+j) <= array1.length()) {
        if(array1[i+j] == array2[j])
            check = true;
        else
            check = false;
      }
   }
   if(check)
       return true; // returns true if all array2 == some part of array1
 }
 return false;

您还可以查看Boyer-Moore algorithm 来改进这一点。它用于字符串匹配,但在这里可以应用相同的逻辑。

基本思想是拥有一个数组 2 的查找表,并且能够“跳过”您知道不必再次检查的值。

1   2   3  4  5  6
3   4   5
^-------^  lookup table sees that the offset is 3 to match array2[0] with array1[2]

    1   2   3  4  5  6
 skip to--->3  4  5
    would be the next iteration

【讨论】:

  • 非常感谢。你的解释真的帮助了我,正是我所需要的。出于好奇,我的老师提到循环数组有一个特殊的属性,可以在线性时间内检查这个相等性。你会知道这件事吗?
  • 原始数组中是否有重复值?实际上这个算法不是 0(n^2) 我想我弄错了它实际上是 0(n*m) 其中 n 是第一个数组,m 是第二个数组。随着 n 大小的增加,摊销时间变为 0(n)。我还在底部添加了一些东西来使这个算法更快
  • 在我的问题中不应该有任何重复。但是我老师提到的问题,我不确定。他说的一般是2个循环数组。改进后的算法应该是 O(3n)。
【解决方案2】:

假设这个算法可以帮助你。

public void test() {
    int[] a1 = {1,2,3,4};
    int[] a2 = {2,3,4,5};
    int[] a3 = {2,3,4,1};
    if (calculateDifference(a1, a2)) {
        System.out.println("a1 has same elements order to a2");
    }
    if (calculateDifference(a1, a3)) {
        System.out.println("a1 has same elements order to a3");
    }
    if (calculateDifference(a2, a3)) {
        System.out.println("a2 has same elements order to a3");
    }
}
private boolean calculateDifference(int[] a1,int[] a2){
    int total = 0;
    boolean match = false;
    if (a1.length != a2.length) {
        return match;
    }
    for (int i = 0; i < a1.length; i++) {
        int a1Num = a1[i];
        int a2Num = a2[i];
        total += a1Num - a2Num;
    }
    if (total == 0) {
        match = true;
    }
    return match;
}

【讨论】:

  • 这个算法没有考虑元素的顺序。例如:calculateDifference(new int[]{1,3,2,4}, new int[]{1,2,3,4}) 是 true。应该是false
  • 乔说 {1,3,2,4} 应该是假的,因为它的顺序不同。
【解决方案3】:

如果不允许重复,你只需要在第二个数组中找到另一个元素等于第一个数组的第一个元素并从那里检查它们,解决方案是 O(n):

boolean areEquivalent(int[] array1, int[] array2) {
  int i1 = 0, i2 = 0;
  for (; i2 < array2.length; ++i2)
    if (array2[i2] == array1[i1])
     break;

  // no element found in common, they can't be equivalent
  if (i2 == array2.length)
    return false;

  for (int j = 0; j < array1.length; ++j)
    if (array1[i1+j] != array2[(i2+j) % array2.length]
      return false;

  return true;
}

如果允许重复,您必须考虑到i1 和i2 都可以从不同的点开始并尝试所有这些,如果最后一个for 失败,您应该将i1 更改为从第二个开始出现相同的值等等。完成此操作后,您必须更改 i2 并再次重试,这样不同的方法(如 Jay 提出的方法)会更好。

【讨论】:

    【解决方案4】:

    我已经尝试过这样并且它有效:

        int a[] = {2,3,4,1};
        int b[] = {3,4,1,2};  // return true for these input
    
        boolean check = false;
        for (int i = 0; i < a.length; i++) {
            int tmp = b[0];
            System.arraycopy(b, 1, b, 0, b.length - 1);
            b[b.length - 1] = tmp;
            if (Arrays.equals(a, b)) {
                check = true;
                System.out.println("Output: " + Arrays.equals(a, b));
            }
        }
        if (!check) {
            System.out.println("Output: false");
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-10
      • 2020-12-27
      • 1970-01-01
      • 2022-12-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多