-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoSum.java
More file actions
29 lines (25 loc) · 803 Bytes
/
TwoSum.java
File metadata and controls
29 lines (25 loc) · 803 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
package LeetCodeOJ;
import java.util.Hashtable;
public class TwoSum {
public static int[] towSum(int[] numbers, int target) {
int a[] = new int[2];
Hashtable<Integer, Integer> num = new Hashtable<Integer, Integer>();
for (int i = 0; i < numbers.length; i++) {
if (num.containsKey(numbers[i])) {
a[0] = num.get(numbers[i]) + 1;
a[1] = i + 1;
break;
}
num.put(target - numbers[i], i);
}
return a;
}
public static void main(String[] args) {
int numbers[] = { 2, 7, 11, 15 };
int target = 9;
int[] result = towSum(numbers, target);
for (int i = 0; i < result.length; i++) {
System.out.println(result[i]);
}
}
}