Skip to content

Commit 83263a6

Browse files
author
Tushar Borole
committed
167. Two Sum II - Input array is sorted
1 parent 6ce8e39 commit 83263a6

File tree

3 files changed

+79
-64
lines changed

3 files changed

+79
-64
lines changed

.idea/workspace.xml

Lines changed: 55 additions & 64 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
| 590 | [N-ary Tree Postorder Traversal](https://leetcode.com/problems/n-ary-tree-postorder-traversal/) | [Solution](postorder_traversal.js) | 616 ms | 80.9 MB | Easy |
5252
| 783 | [Minimum Distance Between BST Nodes](https://leetcode.com/problems/minimum-distance-between-bst-nodes/) | [minimum_distance_between_bst_nodes.js](minimum_distance_between_bst_nodes.js) | 88 ms | 34.7 MB | Easy |
5353
| 1013 | [Partition Array Into Three Parts With Equal Sum](https://leetcode.com/problems/partition-array-into-three-parts-with-equal-sum/) | [partition_array_into_three_parts.js](partition_array_into_three_parts.js) | 72 ms | 39.6 MB | Easy |
54+
| 167 | [Two Sum II - Input array is sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/) | [two_sum_II.js](two_sum_II.js) | 64 ms | 35.5 MB | Easy |
5455
| 912 | [Sort an Array](https://leetcode.com/problems/sort-an-array/) | [Solution](sort_an_array.js) | 7176 ms | 38.9 MB | Medium |
5556
| 414 | [Third Maximum Number](https://leetcode.com/problems/third-maximum-number/) | [third_maximum_number.js](third_maximum_number.js) | 76 ms | 38 MB | Easy |
5657
| 701 | [Insert into a Binary Search Tree](https://leetcode.com/problems/insert-into-a-binary-search-tree/) | [Solution](insert_into_a_binary_search_tree.js) | 112 ms | 41.9 MB | Medium |

two_sum_II.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* @param {number[]} numbers
3+
* @param {number} target
4+
* @return {number[]}
5+
*/
6+
var twoSum = function(numbers, target) {
7+
let hash = {};
8+
9+
for (let i = 0; i < numbers.length; i++) {
10+
let current = numbers[i];
11+
let diff = target - current; //?
12+
if (hash.hasOwnProperty(diff)) {
13+
let index1 = i + 1;
14+
let index2 = hash[diff] + 1;
15+
return [Math.min(index1, index2), Math.max(index1, index2)];
16+
}
17+
hash[current] = i;
18+
}
19+
20+
return [-1, -1];
21+
};
22+
23+
twoSum([-1, 0], -1); //?

0 commit comments

Comments
 (0)