Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Typescript version
Typescript version of #1-two-sum
  • Loading branch information
agb committed Aug 9, 2022
commit 44d90ce4fd5aad0d7bdb2fd2cc33d65a01eba4ed
29 changes: 29 additions & 0 deletions src/two-sum/res.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* 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.
*
* two-sum.js
*
* @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;
};