MCPcopy Index your code
hub / github.com/HuberTRoy/leetCode / twoSum

Function twoSum

Array/twoSum.js:7–29  ·  view source on GitHub ↗
(nums, target)

Source from the content-addressed store, hash-verified

5 */
6// 简单的O(1)查找。
7 var twoSum = function(nums, target) {
8 let dicts = {}
9
10 for (let [index, data] of nums.entries()) {
11 dicts[data] ? dicts[data].push(index) : (dicts[data] = [index])
12 }
13
14
15 for (let i of nums) {
16 if (dicts[target - i]) {
17 if (target-i === i) {
18 if (dicts[i].length === 2) {
19 return [dicts[i][0], dicts[i][1]]
20 } else {
21 continue
22 }
23
24 }
25
26 return [dicts[i][0], dicts[target-i][0]]
27 }
28 }
29};

Callers

nothing calls this directly

Calls 1

pushMethod · 0.45

Tested by

no test coverage detected