forked from zaiyunduan123/leetcode-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoSum_1.java
More file actions
32 lines (27 loc) · 825 Bytes
/
TwoSum_1.java
File metadata and controls
32 lines (27 loc) · 825 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
package kSum;
import java.util.HashMap;
/**
* @Author jiangyunxiong
* @Date 2019/1/14 下午8:14
*
* 两数之和
*
* 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那两个整数,并返回他们的数组下标。
*/
public class TwoSum_1 {
public int[] twoSum(int[] nums, int target) {
if (nums == null) {
return null;
}
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
map.put(nums[i], i);
}
for (int i = 0; i < nums.length; i++) {
if (map.get(target - nums[i]) != null && map.get(target - nums[i]) != i) {
return new int[]{i, map.get(target - nums[i])};
}
}
return null;
}
}