Skip to content

Commit 6db3dd2

Browse files
author
baochau.dinh
committed
js-concepts: leetcode prob.167 - two sum in sorted array
1 parent d6c4d91 commit 6db3dd2

File tree

1 file changed

+14
-0
lines changed
  • LeetCode/167. Two Sum II - Input Array Is Sorted

1 file changed

+14
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var twoSum = function(numbers, target) {
2+
let first = 0, second = numbers.length - 1;
3+
while (first < second) {
4+
if (numbers[first] + numbers[second] < target) {
5+
first++;
6+
} else if (numbers[first] + numbers[second] > target) {
7+
second--;
8+
} else {
9+
return [first + 1, second + 1];
10+
}
11+
}
12+
};
13+
14+
console.log(twoSum([2, 3, 4], 6));

0 commit comments

Comments
 (0)