Skip to content

Commit 3ec367d

Browse files
authored
Merge pull request #2023 from Sol35229/main
[Sol35229] WEEK 01 solutions
2 parents 76ff87f + 4ddd37e commit 3ec367d

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

two-sum/Sol35229.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
public class Sol35229 {
5+
6+
public int[] bruteForce(int[] nums, int target) {
7+
for (int i = 0; i < nums.length; i++) {
8+
for (int j = i+1; j < nums.length; j++) {
9+
if (nums[i]+nums[j] == target) {
10+
return new int[]{i, j};
11+
}
12+
}
13+
}
14+
return new int[] {};
15+
}
16+
17+
public int[] hashTable(int[] nums, int target) {
18+
Map<Integer, Integer> hashMap = new HashMap<>();
19+
for (int i = 0; i < nums.length; i++) {
20+
hashMap.put(nums[i], i);
21+
}
22+
for (int i = 0; i < nums.length; i++) {
23+
int complement = target - nums[i];
24+
if (hashMap.containsKey(complement) && hashMap.get(complement) != i) {
25+
return new int[]{i, hashMap.get(complement)};
26+
}
27+
}
28+
return new int[] {};
29+
}
30+
}
31+

0 commit comments

Comments
 (0)