-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Time: 50 ms (88.45%), Space: 50.2 MB (5.77%) - LeetHub
- Loading branch information
1 parent
1a22e4f
commit cd77b06
Showing
1 changed file
with
29 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
0167-two-sum-ii-input-array-is-sorted/0167-two-sum-ii-input-array-is-sorted.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* @param {number[]} numbers | ||
* @param {number} target | ||
* @return {number[]} | ||
*/ | ||
// var twoSum = function(numbers, target) { | ||
|
||
// }; | ||
function twoSum(numbers, target) { | ||
let l = 0; | ||
let r = numbers.length - 1; | ||
|
||
while (l < r) { | ||
if (numbers[l] + numbers[r] === target) { | ||
return [l + 1, r + 1]; | ||
} else if (numbers[l] + numbers[r] > target) { | ||
r--; | ||
} else { | ||
l++; | ||
} | ||
} | ||
|
||
return []; | ||
} | ||
|
||
// Example usage: | ||
// let numbers = [2, 7, 11, 15]; | ||
// let target = 9; | ||
// console.log(twoSum(numbers, target)); // Output: [1, 2] |