-
Notifications
You must be signed in to change notification settings - Fork 24
/
0001-two-sum.js
45 lines (35 loc) · 1.03 KB
/
0001-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
// 1. Two Sum
// Easy 35%
// 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:
// Given nums = [2, 7, 11, 15], target = 9,
// Because nums[0] + nums[1] = 2 + 7 = 9,
// return [0, 1].
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
const twoSum = function(nums, target) {
const hash = {}
for (let i = 0, n = nums.length; i < n; i++) {
if (hash[nums[i]] != null) return [hash[nums[i]], i]
hash[target - nums[i]] = i
}
return [-1, -1]
}
;[
[[2, 7, 11, 15], 9] // [0, 1]
].forEach(args => {
console.log(twoSum(...args))
})
// Solution:
// 使用哈希表,以每位数的互补数为键名,该数的位置为值。
// 如果一个数对应等于一个键名,说明找到了互补数。
// (哈希的应用)
// 时间复杂度 O(n)
// 空间复杂度 O(n)
// Submission Result: Accepted