forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwoSumIIIDataStructureDesign.java
37 lines (32 loc) · 1.03 KB
/
TwoSumIIIDataStructureDesign.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
package easy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//Your TwoSum object will be instantiated and called as such:
//TwoSum twoSum = new TwoSum();
//twoSum.add(number);
//twoSum.find(value);
public class TwoSumIIIDataStructureDesign {
private Map<Integer, Integer> map = new HashMap();
private List<Integer> list = new ArrayList();
// Add the number to an internal data structure.
public void add(int number) {
list.add(number);
map.put(number, map.getOrDefault(number, 0)+1);
}
// Find if there exists any pair of numbers which sum is equal to the value.
public boolean find(int value) {
for(int i = 0; i < list.size(); i++){
int val1 = list.get(i);
int val2 = value-val1;
if(map.containsKey(val2)) {
if(val1 == val2) {
if(map.get(val2) > 1) return true;
}
else return true;
}
}
return false;
}
}