Javascript, solution-sharing


  • 0
    C
    /**
     * @param {number[]} nums
     * @param {number} target
     * @return {number[]}
     */
    var twoSum = function(nums, target) {
        var hashTable = {},
            result = [],
            carry = 0;
        for(var i=0; i<nums.length; i++){
            carry = (typeof hashTable[target-nums[i]]==="undefined")?(hashTable[nums[i]]=i, 0):(result=[hashTable[target-nums[i]]+1, i+1], 1);
            if(carry){ return result; }
        }
        return null;
    };

Log in to reply
 

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