class TwoSum {
private List<Integer> values = new ArrayList<>();
// private Set<Integer> values = new HashSet<>();
/** Initialize your data structure here. */
public TwoSum() {
}
/** Add the number to an internal data structure.. */
public void add(int number) {
values.add(number);
}
/** Find if there exists any pair of numbers which sum is equal to the value. */
public boolean find(int value) {
// Integer result = -(v - value);
// if (values.contains(result)) {
// return true;
// }
Set<Integer> set = new HashSet<>();
for (Integer v : values) { // 1, 3, 5 (4) -
if (set.contains(v)) {
return true;
} else {
Integer result = -(v - value);
set.add(result);
}
}
return false;
}
}
/**
* Your TwoSum object will be instantiated and called as such:
* TwoSum obj = new TwoSum();
* obj.add(number);
* boolean param_2 = obj.find(value);
*/