Javascript solution using dictionary


  • 0
    R
    /**
     * @param {number[]} nums
     * @param {number} target
     * @return {number[]}
     */
    var twoSum = function(nums, target) {
        var number =[];
        if(nums == null||nums.length<2){return number;}
        var map = [];
        for(var i=0;i<nums.length;i++){
            var val = target - nums[i];
            if(map[val] != null){
                number[0]=map[val];
                number[1]=i;
                return number;
            }else{
                map[nums[i]]=i;
            }
        }   
            
        return number;
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.