-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwoSum.java
38 lines (35 loc) · 1017 Bytes
/
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
package brian;
import java.util.HashMap;
/**
* Created by brian on 8/13/17.
*/
public class TwoSum {
public int[] twoSumByHashMap(int[] nums, int target) {
HashMap<Integer, Integer> map = new HashMap<>();
// [2, 7, 4, 3]
// {
// 2: 0,
// 7: 1,
// 4: 2,
// 3: 3
// }
for (int i = 0; i < nums.length; i++) {
int x = nums[i];
if (map.containsKey(target - x)) {
return new int[] { map.get(target - x), i };
}
map.put(x, i);
}
throw new IllegalArgumentException("no solution!!!");
}
public int[] twoSum(int[] nums, int target) {
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] == target) {
return new int[] {i, j};
}
}
}
throw new IllegalArgumentException("no solution!!!");
}
}