https://leetcode-cn.com/problems/two-sum/submissions/
public int[] twoSum(int[] arr, int target){
if(arr==null){
return null;
}
Map<Integer,Integer> map = new HashMap();
for (int i = 0; i < arr.length; i++) {
map.put(arr[i],i);
}
int[] ret = new int[2];
for (int i = 0; i < arr.length; i++) {
int key = target -arr[i];
if(map.containsKey(key)&&map.get(key)!=i){
ret[0]=map.get(key);
ret[1] = i;
return ret ;
}
}
return ret;
}
问题
1、注意不能是俩个相同的下标相加
2、这种算法快本质上还是用到了 hashmap o(1)的查询效率