-
Notifications
You must be signed in to change notification settings - Fork 0
/
1. Two Sum.js
56 lines (52 loc) · 1.25 KB
/
1. Two Sum.js
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/**
* Note:
* Solution A:
* 1. Brute force: O(N^2)
* Solution B:
* 1. Sort array
* 2. Two pointers: O(NlogN)
* Solution C:
* 1. Map(target - nums[i], i): O(N)
* 2. iterate the array and check hash map
* 3. append hash value and current index into the result array
*/
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
function twoSum(nums, target) {
if (nums.length === 0 || target === null) {
return null;
}
let map = new Map();
let result = [];
for (let i = 0; i < nums.length; i++) {
if (map.size !== 0) {
if (map.has(nums[i])) {
result.push(map.get(nums[i]));
result.push(i);
return result;
}
}
map.set(target - nums[i], i);
}
}
/**
* Leetcode Fundamental: Map, Update 1/21/2019
*/
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function(nums, target) {
let map = new Map();
// Map stores target - num as key and curr index as value
for (let i = 0; i < nums.length; i += 1) {
if (map.has(nums[i])) return [map.get(nums[i]), i];
let key = target - nums[i];
map.set(key, i);
}
return [];
};