-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoSum.java
More file actions
34 lines (29 loc) · 717 Bytes
/
TwoSum.java
File metadata and controls
34 lines (29 loc) · 717 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
31
32
33
34
package code.leetcode.easy.array;
import java.util.HashMap;
import java.util.Map;
public class TwoSum {
public static void main(String[] args) {
int[] nums = { 3, 2, 4 };
int target = 6;
twoSum(nums, target);
System.out.println(-1 << 29);
System.out.println(0 << 29);
System.out.println(1 << 29);
System.out.println(2 << 29);
System.out.println(3 << 29);
}
public static int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> s = new HashMap<Integer, Integer>();
int[] r = new int[2];
for (int i = 0; i < nums.length; i++) {
int t = target - nums[i];
if (s.containsKey(nums[i])) {
r[0] = s.get(nums[i]);
r[1] = i;
break;
}
s.put(t, i);
}
return r;
}
}