Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcf6a92e

Browse files
refactor 170
1 parent9264e3e commitcf6a92e

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
importjava.util.List;
66
importjava.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 aTwoSum 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
publicclass_170 {
28+
publicstaticclassSolution1 {
2829
privateMap<Integer,Integer>map =newHashMap();
2930
privateList<Integer>list =newArrayList();
3031

3132
// Add the number to an internal data structure.
3233
publicvoidadd(intnumber) {
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
publicbooleanfind(intvalue) {
39-
for (inti =0;i <list.size();i++) {
40-
intval1 =list.get(i);
41-
intval2 =value -val1;
42-
if (map.containsKey(val2)) {
43-
if (val1 ==val2) {
44-
if (map.get(val2) >1) {
45-
returntrue;
46-
}
47-
}else {
48-
returntrue;
49-
}
40+
for (inti =0;i <list.size();i++) {
41+
intval1 =list.get(i);
42+
intval2 =value -val1;
43+
if (map.containsKey(val2)) {
44+
if (val1 ==val2) {
45+
if (map.get(val2) >1) {
46+
returntrue;
5047
}
48+
}else {
49+
returntrue;
50+
}
5151
}
52-
returnfalse;
52+
}
53+
returnfalse;
5354
}
55+
}
5456
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp