c++ solution with 2 pointers O(n)


  • 0
    C

    '''
    class Solution {
    public:
    vector<int> twoSum(vector<int>& numbers, int target) {
    int start = 0;
    int end = numbers.size() - 1;
    while(start < end) {
    if(numbers[start] + numbers[end] == target) {
    return {start + 1, end + 1};
    } else if(numbers[start] + numbers[end] < target) {
    start++;
    } else end--;
    }

    }
    

    };
    '''


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.