7 lines concise and easy understand c++ solution


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

Log in to reply
 

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