-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwoSum.java
38 lines (35 loc) · 1.03 KB
/
TwoSum.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
public class TwoSum {
public static void main(String[] args) {
int[] a = {3,2,4};
new TwoSum().twoSum(a, 6);
}
//my solution
public int[] twoSum(int[] nums, int target) {
int idx[] = new int[2];
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (target - nums[i] == nums[j]) {
idx[0] = i;
idx[1] = j;
return idx;
}
}
}
return idx;
}
}
/* best solution
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
map.put(nums[i], i);
}
for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];
if (map.containsKey(complement) && map.get(complement) != i) {
return new int[] { i, map.get(complement) };
}
}
throw new IllegalArgumentException("No two sum solution");
}
*/