原题链接:https://leetcode-cn.com/problems/he-wei-sde-liang-ge-shu-zi-lcof/
双指针
vector<int> twoSum(vector<int>& nums, int target) {
int n = nums.size();
int p = 0, q = n - 1;
vector<int> ans;
while (p < q) {
int sum = nums[p] + nums[q];
if (sum < target) {
p++;
} else if (sum > target) {
q--;
} else {
ans.push_back(nums[p]);
ans.push_back(nums[q]);
return ans;
}
}
return {};
}