-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathres.ts
More file actions
28 lines (24 loc) · 666 Bytes
/
res.ts
File metadata and controls
28 lines (24 loc) · 666 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
27
28
/**
* Problem: 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.
*
*
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
const twoSum =(nums:Array<number>, target:number): Array<number> => {
for (let i = nums.length - 1; i >= 0; i--) {
for (let j = 0; j < i; j++) {
if ( addition(nums[i], nums[j]) === target ) {
return [j, i];
}
}
}
return [];
};
// add a with b and return it
const addition = (a:number, b:number) => {
return a + b;
};