Skip to content

feat: Add javascript Solution for 167 #91

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

Merged
merged 3 commits into from
Jul 19, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,7 @@ _If you like this project, please leave me a star._ ★
|170|[Two Sum III - Data structure design](https://leetcode.com/problems/two-sum-iii-data-structure-design/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_170.java)| |Easy
|169|[Majority Element](https://leetcode.com/problems/majority-element/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_169.java)| [:tv:](https://youtu.be/M1IL4hz0QrE)|Easy|
|168|[Excel Sheet Column Title](https://leetcode.com/problems/excel-sheet-column-title/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_168.java)| |Easy|
|167|[Two Sum II - Input array is sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_167.java)| |Easy| Binary Search
|167|[Two Sum II - Input array is sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/)|[Java](../master/src/main/java/com/fishercoder/solutions/_167.java), [Javascript](../master/javascript/_167.js)| |Easy| Binary Search
|166|[Fraction to Recurring Decimal](https://leetcode.com/problems/fraction-to-recurring-decimal/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_166.java) | |Medium| HashMap
|165|[Compare Version Numbers](https://leetcode.com/problems/compare-version-numbers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_165.java)| |Easy|
|164|[Maximum Gap](https://leetcode.com/problems/maximum-gap/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_164.java) | |Hard|
Expand Down
22 changes: 22 additions & 0 deletions javascript/_167.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @param {number[]} numbers
* @param {number} target
* @return {number[]}
*/
var twoSum = function (numbers, target) {
var p1 = 0
var p2 = numbers.length - 1

while (p1 < p2) {
if (numbers[p1] + numbers[p2] < target) {
p1 += 1
} else if (numbers[p1] + numbers[p2] > target) {
p2 -= 1
} else {
return [p1 + 1, p2 + 1]
}
}
return [-1, -1]
}

console.log(twoSum([2, 7, 11, 15], 9))