解题思路:
两数之和的问题主要考察对哈希表的应用。
注意数组给定的是无序的。
class Solution {
public int[] twoSum(int[] nums, int target) {
int n = nums.length;
HashMap HM = new HashMap<>();
//遍历储存
for (int i =0 ; i < n; i++) HM.put(nums[i], i);
//查找
for(int i = 0; i < n; i++){
int res = target - nums[i];
//题目中同一元素在答案里不能重复出现。
if(HM.containsKey(res) && HM.get(res) != i)
return new int[]{i, HM.get(res)};
}
return new int[] {-1, -1};
}
}



