[英]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.