Skip to content

Commit 0e2d804

Browse files
committed
Add solution #167
1 parent c4783c3 commit 0e2d804

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
145|[Binary Tree Postorder Traversal](./0145-binary-tree-postorder-traversal.js)|Easy|
7474
151|[Reverse Words in a String](./0151-reverse-words-in-a-string.js)|Medium|
7575
152|[Maximum Product Subarray](./0152-maximum-product-subarray.js)|Medium|
76+
167|[Two Sum II - Input Array Is Sorted](./0167-two-sum-ii-input-array-is-sorted.js)|Easy|
7677
169|[Majority Element](./0169-majority-element.js)|Easy|
7778
179|[Largest Number](./0179-largest-number.js)|Medium|
7879
191|[Number of 1 Bits](./0191-number-of-1-bits.js)|Easy|
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* 167. Two Sum II - Input Array Is Sorted
3+
* https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/
4+
* Difficulty: Easy
5+
*
6+
* Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order,
7+
* find two numbers such that they add up to a specific target number. Let these two numbers
8+
* be numbers[index1] and numbers[index2] where 1 <= index1 < index2 <= numbers.length.
9+
*
10+
* Return the indices of the two numbers, index1 and index2, added by one as an integer array
11+
* [index1, index2] of length 2.
12+
*
13+
* The tests are generated such that there is exactly one solution. You may not use the same
14+
* element twice.
15+
*/
16+
17+
/**
18+
* @param {number[]} numbers
19+
* @param {number} target
20+
* @return {number[]}
21+
*/
22+
var twoSum = function(numbers, target) {
23+
const map = new Map();
24+
25+
for (let i = 0; i < numbers.length; i++) {
26+
const diff = target - numbers[i];
27+
28+
if (map.has(diff)) {
29+
return [map.get(diff) + 1, i + 1];
30+
}
31+
32+
map.set(numbers[i], i);
33+
}
34+
};

0 commit comments

Comments
 (0)