Skip to content

Commit cf6a92e

Browse files
refactor 170
1 parent 9264e3e commit cf6a92e

File tree

1 file changed

+26
-24
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+26
-24
lines changed

src/main/java/com/fishercoder/solutions/_170.java

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,50 +5,52 @@
55
import java.util.List;
66
import java.util.Map;
77

8-
98
/**
109
* 170. Two Sum III - Data structure design
11-
* <p>
12-
* Design and implement a _1 class. It should support the following operations: add and find.
13-
* <p>
10+
*
11+
* Design and implement a TwoSum class. It should support the following operations: add and find.
12+
*
1413
* add - Add the number to an internal data structure.
1514
* find - Find if there exists any pair of numbers which sum is equal to the value.
16-
* <p>
17-
* For example,
15+
*
16+
* Example 1:
1817
* add(1); add(3); add(5);
1918
* find(4) -> true
2019
* find(7) -> false
20+
*
21+
* Example 2:
22+
* add(3); add(1); add(2);
23+
* find(3) -> true
24+
* find(6) -> false
2125
*/
2226

23-
//Your _1 object will be instantiated and called as such:
24-
//_1 twoSum = new _1();
25-
//twoSum.add(number);
26-
//twoSum.find(value);
2727
public class _170 {
28+
public static class Solution1 {
2829
private Map<Integer, Integer> map = new HashMap();
2930
private List<Integer> list = new ArrayList();
3031

3132
// Add the number to an internal data structure.
3233
public void add(int number) {
33-
list.add(number);
34-
map.put(number, map.getOrDefault(number, 0) + 1);
34+
list.add(number);
35+
map.put(number, map.getOrDefault(number, 0) + 1);
3536
}
3637

3738
// Find if there exists any pair of numbers which sum is equal to the value.
3839
public boolean find(int value) {
39-
for (int i = 0; i < list.size(); i++) {
40-
int val1 = list.get(i);
41-
int val2 = value - val1;
42-
if (map.containsKey(val2)) {
43-
if (val1 == val2) {
44-
if (map.get(val2) > 1) {
45-
return true;
46-
}
47-
} else {
48-
return true;
49-
}
40+
for (int i = 0; i < list.size(); i++) {
41+
int val1 = list.get(i);
42+
int val2 = value - val1;
43+
if (map.containsKey(val2)) {
44+
if (val1 == val2) {
45+
if (map.get(val2) > 1) {
46+
return true;
5047
}
48+
} else {
49+
return true;
50+
}
5151
}
52-
return false;
52+
}
53+
return false;
5354
}
55+
}
5456
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy