-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtwoSumDataStructure.java
More file actions
52 lines (46 loc) · 1.39 KB
/
twoSumDataStructure.java
File metadata and controls
52 lines (46 loc) · 1.39 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.util.*;
// Time complexity: O(1) for add, O(n) for find. Space complesity: O(2n)
// It turns out iterating a list is much faster than iterating a count map
// F**k I got TLE, why?
class TwoSum{
Map<Integer, Integer> map;
List<Integer> list;
/** Initialize */
public TwoSum(){
map = new HashMap<>();
list = new LinkedList<>();
}
/** Add the number to an internal data structure.. */
public void add(int number){
if(map.containsKey(number)){
map.put(number, map.get(number)+1);
}else{
map.put(number,1);
list.add(number);
}
}
/** Find if there exists any pair of numbers which sum is equal to the value. */
public boolean find(int target){
for (int i=0; i<list.size(); i++){
int num1 = list.get(i);
int num2 = target-num1;
if((num1 == num2 && map.get(num1)>1) || (num1 != num2 && map.containsKey(num2))){
return true;
}
}
return false;
}
}
public class twoSumDataStructure {
public static void main(String[] args){
TwoSum obj = new TwoSum();
obj.add(1);
obj.add(7);
obj.add(5);
obj.add(1);
System.out.println(obj.map);
System.out.println(obj.find(2));
System.out.println(obj.find(8));
System.out.println(obj.find(9));
}
}