简体   繁体   English

如何检查一个数组是否具有另一个数组的平方元素(无论顺序)?

[英]How to check if an array has squared elements of another array (regardless of order)?

a = [121, 144, 19, 161, 19, 144, 19, 11] a = [121, 144, 19, 161, 19, 144, 19, 11]
b = [121, 14641, 20736, 361, 25921, 361, 20736, 361] returns true, coz b[] has squared values of elements in a[], regardless of order. b = [121, 14641, 20736, 361, 25921, 361, 20736, 361] 返回 true,因为 b[] 具有 a[] 中元素的平方值,无论顺序如何。

Here's what I tried...这是我尝试过的...

static boolean comp(int a[], int b[]) {
        int x = 0;
        if (a.equals(b)) {
            for (int i : a) {
                for (int j : b) {
                    if (a[i] == b[j] * b[j]) {
                        x++;
                    } else {
                        x--;
                    }
                }
            }
        } else {
            return false;
        }
        if (x == a.length) {
            return true;
        } else {
            return false;
        }
    }

Simply this would work:简单地这将工作:

static boolean comp(int a[], int b[]) {

    for (int i : a) {
        if (!Arrays.asList(b).contains(i*i)) {
            return false;
        }
    }
    return true;
}

In short, you go through every value of a and b to see if the value of a squared is b.简而言之,您遍历 a 和 b 的每个值,以查看 a 平方的值是否为 b。 When you find a non-match, you automatically return false.当您发现不匹配时,您会自动返回 false。 Otherwise, true is returned.否则,返回 true。

public static Boolean compare(int[] a, int[] b) { Arrays.sort(a); Arrays.sort(b); for (int i = 0; i < a.length; i++) { if (a[i] * a[i] != b[i]) { return false; } } return true; }
static boolean comp(int a[], int b[]) {

    for (int i : a) {
        if (!Arrays.asList(b).contains(i*i)) {
            return false;
        }
    }
    return true;
}

Try using sets. 尝试使用集合。

      int[] a = { 121, 144, 19, 161, 19, 144, 19, 11
      };
      int[] b = { 121, 14641, 20736, 361, 25921, 361, 20736, 361
      };
      Set<Integer> seta = Arrays.stream(a).map(r -> r * r).boxed().collect(
            Collectors.toSet());
      Set<Integer> setb =
            Arrays.stream(b).boxed().collect(Collectors.toSet());
      System.out.println(setb.containsAll(seta));

Note that this does not take into consideration duplicates. 请注意,这没有考虑重复项。

List has a containsAll , I would use that; List有一个containsAll ,我会使用它; with Java 8+ and streams that might look like使用 Java 8+ 和可能看起来像的流

static boolean comp(int a[], int b[]) {
    return Arrays.stream(b).distinct().boxed().collect(Collectors.toList())
            .containsAll(Arrays.stream(a).map(x -> x * x).boxed()
            .collect(Collectors.toList()));
}

Try sorting both arrays and iterating over both of them at the same, first squaring the number from the first array and then checking if it is equal to the element from the second array.尝试对两个数组进行排序并同时对它们进行迭代,首先将第一个数组中的数字平方,然后检查它是否等于第二个数组中的元素。 Once you reach non-equal elements return false, otherwise return true.一旦到达不相等的元素返回false,否则返回true。 This should give you 2*n*log(n) + n if using quicksort.如果使用快速排序,这应该给你 2*n*log(n) + n。

public static Boolean compare(int[] a, int[] b) {
  Arrays.sort(a);
  Arrays.sort(b);
  for (int i = 0; i < a.length; i++) {
    if (a[i] * a[i] != b[i]) {
      return false;
    }
  }
  return true;
}
bool Same(int[] arr1, int[] arr2)
{
    for (int i = 0; i < arr1.Length; i++)
    {
        var correctIndex = Array.IndexOf(arr2, (int)Math.Pow(arr1[i], 2));
        if (correctIndex == -1)
        {
            return false;
        }
        arr2.Take(correctIndex);
    }
    return true;
}

in c#

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

相关问题 测试数组中的元素是否相等,而与顺序或频率无关 - Testing if elements in an array are equal regardless of order or frequency 如何检查一个列表是否是另一个列表的子集,而不考虑顺序 - How to check whether a list is a subset of another list, regardless of order 如何检查 2D 数组中的 2 个元素是否彼此相邻 - How to check if 2 elements are adjacent to one another in 2D array 将一个数组的元素以相反的顺序存储到另一个数组中 - Storing elements of one Array into another in Reverse Order 检查数组是否具有连续元素? - check whether the array has a consecutive elements or not? 当数组继续时,如何检查在数组中输入的值是否平方? - How do I check if the values that I enter in an array are squared as the array continues? 如何将数组元素分配到另一个数组中,而没有数组的另一个元素多于 1 个? - How to distribute array elements into another array while no array has more than 1 element that the other? Java比较数组数据的大小和顺序 - Java comparison of array data regardless of size or order 如何检查一个数组中的所有元素是否存在于另一个数组中。 但是以相同的顺序 - How to check if all elements in one array exist in other array. However in the same order 如何检查数组元素是否相同 - How to check if array elements are the same
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM