Content-Length: 393886 | pFad | http://github.com/TheAlgorithms/JavaScript/pull/1226/files/424b23547b10af9da1a1cfbc5469df10e09b8067

D0 Two sum by nosecreek · Pull Request #1226 · TheAlgorithms/JavaScript · GitHub
Skip to content

Two sum #1226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open

Two sum #1226

Show file tree
Hide file tree
Changes from 3 commits
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
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
* [LocalMaximomPoint](Data-Structures/Array/LocalMaximomPoint.js)
* [NumberOfLocalMaximumPoints](Data-Structures/Array/NumberOfLocalMaximumPoints.js)
* [QuickSelect](Data-Structures/Array/QuickSelect.js)
* [TwoSum](Data-Structures/Array/TwoSum.js)
* **Graph**
* [Graph](Data-Structures/Graph/Graph.js)
* [Graph2](Data-Structures/Graph/Graph2.js)
Expand Down
24 changes: 24 additions & 0 deletions Data-Structures/Array/TwoSum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// https://leetcode.com/problems/two-sum/

// Given an array of integers, return indices of the two numbers such that they add up to
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you try to use a JSDoc comment right above the definition of TwoSum?

// a specific target.

// You may assume that each input would have exactly one solution, and you may not use the
// same element twice.

// Example:
// Given nums = [2, 7, 11, 15], target = 9,

// Because nums[0] + nums[1] = 2 + 7 = 9,
// return [0, 1].

const TwoSum = (nums, target) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a pretty naive implementation with a runtime of O(n²). Please document this in the JSDoc comment.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see the docs and use the proper @annotations.

for (let i = 0; i < nums.length; i++) {
for (let j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] === target) {
return [i, j]
}
}
}
}
export { TwoSum }
11 changes: 11 additions & 0 deletions Data-Structures/Array/test/TwoSum.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { TwoSum } from '../TwoSum'

describe('TwoSum tests', () => {
it('check that a variety of inputs return the correct answer', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just use test("Two Sum", () => ...) right away

expect(TwoSum([2, 7, 11, 15], 9)).toEqual([0, 1])
expect(TwoSum([2, 7, 11, 15, 6], 8)).toEqual([0, 4])
expect(TwoSum([1, 0, 5, 7, 3, 4], 6)).toEqual([0, 2])
expect(TwoSum([0, 8, 3, 1, 2, 7, 3], 6)).toEqual([2, 6])
expect(TwoSum([0, 5, 4, 2, 6, 7, 9, 1], 3)).toEqual([3, 7])
})
})








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/TheAlgorithms/JavaScript/pull/1226/files/424b23547b10af9da1a1cfbc5469df10e09b8067

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy