题目
给定 nums = [2, 7, 11, 15], target = 9
因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]var twoSum = function(nums, target) {
let hash = {};
let temp;
let len = nums.length;
for (let i = 0; i < len; i++) {
hash[nums[i]] = i;
}
for (let j = 0; j < len; j++) {
temp = target - nums[j];
if(hash[temp] && hash[temp] !== j) {
return [j, hash[temp]];
}
}
console.log("Not found.");
};Last updated
Was this helpful?