forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_170.java
54 lines (48 loc) · 1.47 KB
/
_170.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 170. Two Sum III - Data structure design
* <p>
* Design and implement a _1 class. It should support the following operations: add and find.
* <p>
* add - Add the number to an internal data structure.
* find - Find if there exists any pair of numbers which sum is equal to the value.
* <p>
* For example,
* add(1); add(3); add(5);
* find(4) -> true
* find(7) -> false
*/
//Your _1 object will be instantiated and called as such:
//_1 twoSum = new _1();
//twoSum.add(number);
//twoSum.find(value);
public class _170 {
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;
}
}