Skip to content

Commit aec6f6c

Browse files
authored
feat: Typescript hijiangtao#1 two sum (hijiangtao#1)
1 parent 9499040 commit aec6f6c

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
88

99
| ID | Title | Solution | Difficulty |
1010
|-----|-------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------|------------|
11-
| 1 | [Two Sum](https://leetcode.com/problems/two-sum/) | [JavaScript](./src/two-sum/res.js) | Easy |
11+
| 1 | [Two Sum](https://leetcode.com/problems/two-sum/) | [JavaScript](./src/two-sum/res.js)· [TypeScript](./src/two-sum/res.ts) | Easy |
1212
| 2 | [Add Two Numbers](https://leetcode.com/problems/add-two-numbers/) | [JavaScript](./src/add-two-numbers/res.js) | Medium |
1313
| 3 | [Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/) | [JavaScript](./src/longest-substring-without-repeating-characters/res.js) | Medium |
1414
| 4 | [Median of Two Sorted Arrays](https://leetcode.com/problems/median-of-two-sorted-arrays/) | [JavaScript](./src/median-of-two-sorted-arrays/res.js) | Hard |

‎src/two-sum/res.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Problem: Given an array of integers, return indices of the two numbers such that they add up to a specific target.
3+
*
4+
* You may assume that each input would have exactly one solution, and you may not use the same element twice.
5+
*
6+
*
7+
* @param {number[]} nums
8+
* @param {number} target
9+
* @return {number[]}
10+
*/
11+
12+
const twoSum =(nums:Array<number>, target:number): Array<number> => {
13+
14+
for (let i = nums.length - 1; i >= 0; i--) {
15+
for (let j = 0; j < i; j++) {
16+
if ( addition(nums[i], nums[j]) === target ) {
17+
return [j, i];
18+
}
19+
}
20+
}
21+
22+
return [];
23+
};
24+
25+
// add a with b and return it
26+
const addition = (a:number, b:number) => {
27+
return a + b;
28+
};

0 commit comments

Comments
 (0)