Leetcode - 167. Two Sum II - Input Array Is Sorted

Javascript Code /** * @param {number[]} numbers * @param {number} target * @return {number[]} */ var twoSum = function(numbers, target) { let p1 = 0; let p2 = numbers.length - 1; while ( p1 target){ p2--; }else{ p1++ } } return [p1+1,p2+1] };

Mar 1, 2025 - 17:40
 0
Leetcode - 167. Two Sum II - Input Array Is Sorted

Javascript Code

/**
 * @param {number[]} numbers
 * @param {number} target
 * @return {number[]}
 */
var twoSum = function(numbers, target) {

    let p1 = 0;
    let p2 = numbers.length - 1;

    while ( p1 < p2){
        if(numbers[p1] + numbers[p2] == target){
            break;
        }
        if(numbers[p1] + numbers[p2] > target){
            p2--;
        }else{
            p1++
        }
    }
    return [p1+1,p2+1]
};