标题NC61.两数之和![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/0981d924f7ffc4694d4fbe1c5d3ea7f9.png)
import java.util.HashMap;
import java.util.Map;
public class Solution {
/**
* @param numbers int整型一维数组
* @param target int整型
* @return int整型一维数组
*/
public int[] twoSum(int[] numbers, int target) {
// write code here
Map<Integer, Integer> map = new HashMap<>();
int i = 1;
int ans[] = new int[2];
for (int num : numbers) {
//判断map中是否有数与当前数之和等于目标值
if (map.containsKey(target - num)) {
ans[0] = map.get(target - num);
ans[1] = i;
return ans;
}
map.put(num, i++);
}
return null;
}
}