-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
49 lines (39 loc) · 1.17 KB
/
index.ts
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
#!/usr/bin/env deno
/**
* Runtime: 184 ms, faster than 22.83% of TypeScript online submissions for Two Sum.
* Memory Usage: 37.3 MB, less than 51.18% of TypeScript online submissions for Two Sum.
* 16/08/2019
* DD/MM/YY
*/
const count = <T>(target: T, list: T[]): number => {
let total = 0
for (let i = 0; i < list.length; i++) {
if (list[i] === target) {
total ++
}
}
return total
}
const indice = <T>(target: T, list: T[]): number[] => {
let indices: number[] = [],
i = -1
while ((i = list.indexOf(target, i + 1)) !== -1) {
indices.push(i)
}
return indices
}
const twoSum = (nums: number[], target: number): [number, number] | void => {
for (let x = 0; x < nums.length; x++) {
let i = nums[x]
if (nums.includes(target-i)) {
if (count(target-i, nums) >= 2) {
let indices = indice(target-i, nums)
return [indices[0], indices[1]]
} else if (nums.indexOf(i) === nums.indexOf(target-i)) {
continue
} else {
return [nums.indexOf(i), nums.indexOf(target-i)]
}
}
}
}