-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathtwoSum.java
37 lines (30 loc) · 939 Bytes
/
twoSum.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class TwoSum {
HashMap<Integer, Integer> map = new HashMap<>();
/** Initialize your data structure here. */
public TwoSum() {
}
/** Add the number to an internal data structure.. */
public void add(int number) {
if(!map.containsKey(number))
map.put(number,1);
else
map.put(number, map.get(number)+1);
}
/** Find if there exists any pair of numbers which sum is equal to the value. */
public boolean find(int value) {
for(int key: map.keySet())
{
int num1 = key;
int num2 = value - num1;
if( (num1 == num2 && map.get(num1)>1) || (num1 != num2 && map.containsKey(num2)))
return true;
}
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);
*/