Bluetom

LeetCode-1-TwoSum

Problem

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example

1
2
3
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

Solution

1
2
3
4
5
6
7
8
9
10
11
12
const TwoSum = (list, target) => {
let hash = {};
let len = list.length;

for(let j = 0; j < list.length; j++) {
if (list[j] in hash) {
return [hash[list[j]], j];
}
hash[target - list[j]] = j;
}
return [-1, -1];
}

Bluetom

作为挨踢业的前段湿 搬过砖也画过画:爱看、爱听、爱玩儿、爱折腾、爱打撸啊撸、intj

Proudly published with Hexo