forked from rpj911/LeetCode_algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoSum.java
More file actions
26 lines (19 loc) · 633 Bytes
/
TwoSum.java
File metadata and controls
26 lines (19 loc) · 633 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
package Algorithms.array;
import java.util.HashMap;
public class TwoSum {
public int[] twoSum(int[] numbers, int target) {
int[] rst = new int[2];
int len = numbers.length;
HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
for (int i = 0; i< len; i++) {
Integer j = hm.get(target-numbers[i]);
if ((j = hm.get(target-numbers[i])) != null) {
rst[0] = j+1;
rst[1] = i+1;
return rst;
}
hm.put(numbers[i], i);
}
return rst;
}
}