Accepted JavaScript O(n) Solution


  • 0
    N
    /**
     * @param {number[]} nums
     * @param {number} target
     * @return {number[]}
     */
    var twoSum = function(nums, target) {
        var myHashMap = {};
        for(var i = 0; i < nums.length; i++){
            var offset = target - nums[i];
            var index1 = myHashMap[offset];
            if(typeof index1 != "undefined" ){
                var rtn = [index1+1, i+1];
                return rtn;
            }else{
                myHashMap[nums[i]]=i;
            }
        }
        return null;
    };

  • 0
    D

    should fix the case that "nums[index] > nums[i]", right?


Log in to reply
 

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