两数之和
![](https://img2022.cnblogs.com/blog/608798/202211/608798-20221111174005314-239865648.png)
import java.util.*;
public class Solution {
/**
*
* @param numbers int整型一维数组
* @param target int整型
* @return int整型一维数组
*/
public int[] twoSum (int[] numbers, int target) {
// write code here
//复制numbers到map
//遍历数组numbers,值为a,
//计算targer-a,值为b,判断map中是否存在,如果存在,则返回下标
int[] re = new int[2];
Map<Integer,Integer> map = new HashMap<Integer,Integer>();
for(int i=0; i<numbers.length; i++){
map.put(numbers[i],i);
}
for(int j=0;j<numbers.length;j++){
int b = target-numbers[j];
if(map.containsKey(b)&& j!=map.get(b)){
re[0] = j+1;
re[1] = map.get(b)+1;
return re;
}
}
return re;
}
}