[HAI] week3 two sum withou hashsum
https://leetcode.com/problems/two-sum-ii-input-array-is-sorted
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
export function twoSum(numbers: number[], target: number): number[] {
|
||||
let L = 0;
|
||||
let R = numbers.length - 1;
|
||||
|
||||
while (L <= R) {
|
||||
const curSum = numbers[L] + numbers[R];
|
||||
if (curSum === target) return [L + 1, R + 1];
|
||||
curSum > target ? R-- : L++;
|
||||
}
|
||||
|
||||
return [-1, -1];
|
||||
};
|
||||
Reference in New Issue
Block a user