Skip to content

Commit 5733b78

Browse files
Resolved comments and changes requests
1 parent 01539b5 commit 5733b78

File tree

2 files changed

+41
-30
lines changed

2 files changed

+41
-30
lines changed

Maths/TwoSum.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1-
/*
2-
Given an array of integers, return indices of the two numbers such that they add up to
3-
a specific target.
4-
5-
You may assume that each input would have exactly one solution, and you may not use the
6-
same element twice.
7-
8-
Example
9-
Given nums = [2, 7, 11, 15], target = 9,
10-
Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].
11-
return [0, 1].
12-
*/
1+
/**
2+
* Given an array of integers, find two numbers that add up to a specific target.
3+
*
4+
* @param {number[]} nums - The array of integers.
5+
* @param {number} target - The target sum.
6+
* @returns {number[]} - An array containing the indices of the two numbers.
7+
*
8+
* @example
9+
* const nums = [2, 7, 11, 15];
10+
* const target = 9;
11+
* const result = twoSum(nums, target);
12+
* // The function should return [0, 1] because nums[0] + nums[1] = 2 + 7 = 9.
13+
*/
1314

1415
const TwoSum = (nums, target) => {
15-
const numIndicesMap = {}
16+
const numIndicesMap = new Map()
1617
for (let i = 0; i < nums.length; i++) {
1718
const complement = target - nums[i]
18-
if (complement in numIndicesMap) return [numIndicesMap[complement], i]
19-
numIndicesMap[nums[i]] = i
19+
if (numIndicesMap.has(complement)) return [numIndicesMap.get(complement), i]
20+
numIndicesMap.set(nums[i], i)
2021
}
2122
return []
2223
}

Maths/test/TwoSum.test.js

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
import { TwoSum } from '../TwoSum.js'
2-
32
describe('Two Sum', () => {
4-
it('Should find the indices of two numbers that add up to the target', () => {
5-
expect(TwoSum([2, 7, 11, 15], 9)).toEqual([0, 1])
6-
expect(TwoSum([15, 2, 11, 7], 13)).toEqual([1, 2])
7-
expect(TwoSum([2, 7, 11, 15], 17)).toEqual([0, 3])
8-
expect(TwoSum([7, 15, 11, 2], 18)).toEqual([0, 2])
9-
expect(TwoSum([2, 7, 11, 15], 26)).toEqual([2, 3])
10-
expect(TwoSum([2, 7, 11, 15], 8)).toEqual([])
11-
expect(
12-
TwoSum(
13-
[...Array(10).keys()].map((i) => 3 * i),
14-
19
15-
)
16-
).toEqual([])
17-
})
3+
const testCasesWithoutSolution = [
4+
[[8], 8],
5+
[[3, 3, 3, 3], 19]
6+
]
7+
const testCasesWithSolution = [
8+
[[2, 7, 11, 15], 9, [0, 1]],
9+
[[15, 2, 11, 7], 13, [1, 2]],
10+
[[2, 7, 11, 15], 17, [0, 3]],
11+
[[7, 15, 11, 2], 18, [0, 2]],
12+
[[2, 7, 11, 15], 26, [2, 3]]
13+
]
14+
15+
test.each(testCasesWithoutSolution)(
16+
'Should return an empty array if there is no solution',
17+
(nums, target) => {
18+
expect(TwoSum(nums, target)).toEqual([])
19+
}
20+
)
21+
22+
test.each(testCasesWithSolution)(
23+
'Should return the indices of two numbers that add up to the target',
24+
(nums, target, expected) => {
25+
expect(TwoSum(nums, target)).toEqual(expected)
26+
}
27+
)
1828
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy