, 2 , 225, true .
A 225 .
, hashset A [i], true, .
Else, -.
false, .
, A * B = 225
, B = 225 / A
.
, , , "B" .
A hashset, - B, 225/B hashset ( O (1)), , A * B = 225 .
hashset, O (1), O (n) .
Set<Double> mySet = new HashSet<Double>();
for (int i = 0; i < N; i++) {
double val = 225 / (double) A[i];
if (mySet.contains(val)) {
return true;
}
if (!mySet.contains((Double)A[i]) {
mySet.add((Double)A[i]);
}
}
return false;
, . , hashset ( , !) . :
// The below represents the above algo on an array with 10 elements.
// The values 0 ~ 9 are the index.
// The left row is my insert, and top row is my "compare."
// Every intersection "[]" means that the value has been compared to each other.
0 1 2 3 4 5 6 7 8 9
0 [] [] [] [] [] [] [] [] []
1 [] [] [] [] [] [] [] []
2 [] [] [] [] [] [] []
3 [] [] [] [] [] []
4 [] [] [] [] []
5 [] [] [] []
6 [] [] []
7 [] []
8 []
9
// You can see that every value actually compares to each other (except
// for itself. More on this below)
", ", :
15 , , 225 true, "" . ( , )
If your array were simple { 15 }
, do you want to return true
?
If your answer is correct, first switch the code to add, compare the following:
Set<Double> mySet = new HashSet<Double>();
for (int i = 0; i < N; i++) {
double val = 225 / (double) A[i];
if (!mySet.contains((Double)A[i]) {
mySet.add((Double)A[i]);
}
if (mySet.contains(val)) {
return true;
}
}
return false;
source
share