leetcode-两数之和

const target = 9
let nums = [2,11,7,15]
let res = [0,1]
let record_map = new Map() 
function main(){
    if( nums.length<=2)
    {    
         return res
    }
    for (var i = nums.length - 1; i >= 0; i--) 
    {
         if(!record_map.has(target - nums[i])){
             record_map.set(nums[i],i)
         }else{
             res=[]
             res.push(i)
             res.push(record_map.get(target-nums[i]))
             break
         }
    }
    console.log(res) 
    return res
}

 

posted @ 2019-03-26 18:11  NHZ-M  阅读(104)  评论(0编辑  收藏  举报