class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer,Integer> map = new HashMap<>();
for(int i=0;i<nums.length;i+=1){
int num = nums[i];
if(map.get(target-num)!=null){
return new int[]{map.get(target-num),i};
}
else{
map.put(num,i);
}
}
return new int[]{0,0};
}
}
这道题直接使用hashmap 即可