[英]How do I find two values in an array that multiply to a certain number
What I'm trying to do is search through an array and find out if there's two numbers that multiply to 225. Here's what I have right now: 我想做的是搜索一个数组,找出是否有两个数字乘以225。这就是我现在所拥有的:
int n = A.length;
for(int i = 0; i >= n; i++){
for(int j = 0; j >= n; j++){
if(i == j){
}
else if(A[i] * A[j] == 225){
return true;
}
}
}
return false;
}
What happens is it finds the first value in the array and multiplys that with every other value and once it finds two numbers that are 225 then it returns true. 发生的事情是找到数组中的第一个值,并将其与其他每个值相乘,一旦找到两个225的数字,则返回true。 I also made it so that if i and j are the same number then it does nothing because I don't want it comparing values in the same position.
我也这样做了,所以如果i和j是相同的数字,那么它什么都不做,因为我不希望它比较同一位置的值。 The problem is it keeps on returning false even on array's which do have two numbers that multiply to 225 (like 15, 45, 60, 15).
问题在于,即使在数组中确实有两个乘以225的数字(例如15、45、60、15),它也会不断返回false。 So what's wrong with my code.
那么我的代码有什么问题。
you have 1 problem with for loop: 您有1个for循环问题:
it should look like this: 它应该看起来像这样:
int n = A.length;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
if(i != j)
if(A[i] * A[j] == 225)
return true;
also, you could optimize this loop a little bit to: 另外,您可以对该循环进行一些优化以:
int n = A.length;
for(int i = 0; i < (n-1); i++)
for(int j = (i+1); j < n; j++)
if (A[i] * A[j] == 225)
return true;
here is a little bit different version, which has average complexity O(N): 这是一个略有不同的版本,其平均复杂度为O(N):
final Map<Integer, Boolean> set = new HashMap<>(A.length);
for (final int i : A) {
set.put(i, set.containsKey(i)); // store number from array, set to true if there are multiple occurrences of 2 numbers
}
for (final int i : A) {
final int mult = 225 / i;
if ((mult * i) == 225) { // test that integral multiplication leads to 225
final Boolean m = set.get(mult);
if (m != null) { // we have both numbers in set, now we should filter double counting
if ((i != mult) || ((i == mult) && m.booleanValue()))
return true;
}
}
}
return false;
Here Arrays.sort() use quicksort internally 这里Arrays.sort()在内部使用quicksort
public class FindMultiplicationValueUsingpointer
{
private void operation(int [] arr, int required)
{//2, 3, 4, 5, 6, 7
Arrays.sort(arr);
int right =arr.length-1;
int left= 0, val;
while(left<right)
{
System.out.println("left is "+arr[left]);
System.out.println("right is "+arr[right]);
val = arr[left]*arr[right];
System.out.println("value is " +val);
if(val<required)
{
left = left+1;
}
else if(val>required)
{
right=right-1;
}
else if(val==required)
{
System.out.println("value matched");
break;
}
}
}
public static void main(String[] args)
{
int arr[] = {3, 2, 6, 7, 4, 5};
int required = 20;
FindMultiplicationValueUsingpointer up = new FindMultiplicationValueUsingpointer();
up.operation(arr, required);
}
It can also be done for any number with one for loop using hashset which will also minimize the number of iteration . 也可以使用哈希集对任何数字进行一个for循环来完成,这也将最大程度地减少迭代次数。 Best case is o(1) and worst case o(n):
最好的情况是o(1),最坏的情况是o(n):
public boolean hasAnyPair(int[] arr, int n) {
boolean hasPair=false;
HashSet<Integer> set= new HashSet<Integer>();
for(int i=0; i<arr.length; i++) {
if(n>=arr[i] && n%arr[i]==0) {
set.add(arr[i]);
if(set.contains(n/arr[i])) {
hasPair=true;
break;
}
}
}
return hasPair;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.