cpp
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
int n = nums.size();
int left = 0;
int right = n-1;
while(left<right){
if(nums[left]+nums[right]>target) right--;
else if(nums[left]+nums[right]<target) left++;
else return {nums[left],nums[right]};
}
return {-1,-1};
}
};
注意:
return {-1,-1};这句代码是为了照顾力扣的编译器的