Two Sum

作者: ae12 | 来源:发表于2019-05-03 15:58 被阅读0次

use hashmap strore the data
the time complexity is about o(n) cost in the traversal of the int array;and the map cost o(1) if not collistion,however if the collision exist ,time cost is o(n)
the space complexity is o(n) store the value

public int [] twoSum(int [] sums,int target){
Map<Integer,Integer> map =new HashMap();
for(int i=0;i<sum.length;i++){
int complement =tatget - sums[i];
if(map.containKey(complement)){
return new Int[]{i,map.get(complement)}
}
map.put(sum[i],i]);
}

相关文章

网友评论

      本文标题:Two Sum

      本文链接:https://www.haomeiwen.com/subject/bwfznqtx.html