Skip to content

Commit 954babb

Browse files
committed
two sums solution
1 parent a3d7b57 commit 954babb

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

two-sum/leebeanbin.java

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

0 commit comments

Comments
 (0)