Skip to content

Commit e0cd94f

Browse files
committed
twoSums first version
0 parents  commit e0cd94f

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

twoSums.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} target
4+
* @return {number[]}
5+
*/
6+
var twoSum = function(nums, target) {
7+
let cache = {}, index = 0
8+
9+
while(nums.length){
10+
let num = nums.shift(),
11+
otherNum = target - num
12+
if (cache[otherNum] !== undefined){
13+
return [cache[otherNum], index]
14+
}
15+
cache[num] = index
16+
index++
17+
}
18+
};

0 commit comments

Comments
 (0)