c++ two pointer


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

Log in to reply
 

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