AcWing 75. 和为S的两个数字
原题链接
简单
作者:
Saber__
,
2024-11-04 16:55:33
,
所有人可见
,
阅读 2
class Solution {
public:
vector<int> findNumbersWithSum(vector<int>& nums, int target) {
sort(nums.begin(),nums.end());
for(int i = 0,j = nums.size()-1;i<j;)
{
if(nums[i] + nums[j] == target)
return vector<int>{nums[i],nums[j]};
else if(nums[i] + nums[j] < target)
i++;
else
j--;
}
}
};
class Solution {
public:
vector<int> findNumbersWithSum(vector<int>& nums, int target) {
unordered_set<int> S;
for(auto x : nums)
{
if(S.count(target-x)) return {target-x,x};
S.insert(x);
}
}
};