思路:使用hashmap,key为数组中的值,value为数组的角标.
时间复杂度为O(n)
//int[] nums = {1, 3, 4, 6, 2, 3, 8}; int target = 10;
public void test(int[] nums, int target) {
Map<Integer,Integer> temp = new HashMap<>();
for (int i = 0; i < nums.length - 1; i++) {
if (temp.containsKey(target-nums[i])){
System.out.println(i+":"+temp.get(target-nums[i]));
return;
}
temp.put(nums[i],i);
}
}