-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ1TwoSum.java
More file actions
30 lines (27 loc) · 782 Bytes
/
Q1TwoSum.java
File metadata and controls
30 lines (27 loc) · 782 Bytes
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
import java.util.Arrays;
import java.util.HashMap;
/**
* 1. Two Sum
*
* @author ahscuml
* @date 2018/9/19
* @time 20:25
*/
public class Q1TwoSum {
public static void main(String[] args) {
int[] nums = {2, 7, 11, 15};
int target = 9;
System.out.println(Arrays.toString(twoSum(nums,target)));
}
public static int[] twoSum(int[] nums, int target) {
HashMap<Integer,Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];
if (map.containsKey(complement)) {
return new int[] { map.get(complement), i };
}
map.put(nums[i], i);
}
throw new IllegalArgumentException("no such solution");
}
}