forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT1.java
More file actions
32 lines (29 loc) · 754 Bytes
/
T1.java
File metadata and controls
32 lines (29 loc) · 754 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
/**
* @program JavaBooks
* @description: 两数之和
* @author: mf
* @create: 2020/04/10 20:11
*/
package subject.hash;
import java.util.HashMap;
/**
* 给定 nums = [2, 7, 11, 15], target = 9
*
* 因为 nums[0] + nums[1] = 2 + 7 = 9
* 所以返回 [0, 1]
*/
public class T1 {
public int[] twoSum(int[] nums, int target) {
if (nums == null || nums.length == 0) return null;
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int error = target - nums[i];
if (map.containsKey(error)) {
return new int[] {map.get(error), i};
} else {
map.put(nums[i], i);
}
}
return null;
}
}