LeetCode:1.两数之和
新建一个字典作为婚姻介绍所。nums里的值,逐个来介绍所找对象,没有合适的就先登记着,有合适的就牵手成功。
/*** @param {number[]} nums* @param {number} target* @return {number[]}*/
var twoSum = function(nums, target) {let map=new Map()let difflet result=[]nums.forEach((item,index)=>{diff=target-itemif(map.has(diff)){result=[index,map.get(diff)]}else{map.set(item,index)}})return result
};
//time 6ms 54mb O(n)
// es6 api time fast//for time 72ms 35mb
'