用map
public int[] twoSum(int[] numbers, int target) {
int[] result = new int[2];
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < numbers.length; i++) {
if (map.containsKey(target - numbers[i])) {
result[1] = i + 1;
result[0] = map.get(target - numbers[i]);
return result;
}
//如果有重复值,就执行这个
if(map.containsKey(numbers[i])) {
continue;
}
map.put(numbers[i], i + 1);
}
return result;
}