面试题57. 和为s的两个数字
程序员文章站
2024-03-04 11:34:35
...
vector<int> twoSum(vector<int> &nums, int target) {
vector<int> v;
int l = 0, r = nums.size() - 1;
while (l <= r) {
if (nums.at(l) + nums.at(r) == target) {
v.emplace_back(nums.at(l)), v.emplace_back(nums.at(r));
break;
} else if (nums.at(l) + nums.at(r) < target)
l++;
else if (nums.at(l) + nums.at(r) > target)
r--;
}
return v;
}