题目描述
blablabla
样例
blablabla
算法1
(暴力枚举)
C++ 代码
class Solution {
public:
vector<int> findNumbersWithSum(vector<int>& nums, int target) {
for(int i = 0; i < nums.size(); ++i) {
for(int j = i + 1; j < nums.size(); ++j) {
if(nums[i] + nums[j] == target) {
return {nums[i], nums[j]};
}
}
}
return {};
}
};
算法2
(双指针)
C++ 代码
class Solution {
public:
vector<int> findNumbersWithSum(vector<int>& nums, int target) {
sort(nums.begin(), nums.end());
int i = 0, j = nums.size() - 1;
while(i < j) {
if(nums[i] + nums[j] == target) {
return {nums[i], nums[j]};
} else if(nums[i] + nums[j] > target) {
--j;
} else if(nums[i] + nums[j] < target) {
++i;
}
}
return {};
}
};