-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0001_two_sum.js
More file actions
26 lines (26 loc) · 852 Bytes
/
0001_two_sum.js
File metadata and controls
26 lines (26 loc) · 852 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* @link https://leetcode.com/problems/two-sum/
* @description Return indices of the two numbers such that they add up to target.
* @param {Object []} nums
* @param {Number} target
* @returns {Object []}
*/
export default (nums, target) => {
// key: Number that adds up to target
// value: Index the number is at
const twoSum = {};
// Iterate through nums using indices b/c we need the indices
// for the final ads
for (let i = 0; i < nums.length; i += 1) {
const currentNumber = nums[i];
// Case 1: We found a pair that adds up to target
if (currentNumber in twoSum) {
return [twoSum[currentNumber], i];
}
// Case 2: Add a new entry for the current number
twoSum[target - currentNumber] = i;
}
// If we go through the entire for loop without returning
// there's no valid answe
return -1;
};