题目描述
输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的。
输出描述:
对应每个测试案例,输出两个数,小的先输出。
参考代码
class Solution {
public:
vector<int> FindNumbersWithSum(vector<int> data,int sum) {
int length = data.size();
vector<int> res;
if(length == 0)
return res;
int low = 0;
int high = length - 1;
int res1, res2;
bool flag = false;
int mMin = 0x7FFFFFFF;
while(low <= high){
if(data[low] + data[high] == sum){
if(data[low] * data[high] < mMin){
mMin = data[low] * data[high];
res1 = data[low];
res2 = data[high];
flag = true;
}
}
if(data[low] + data[high] > sum)
high--;
else
low++;
}
if(flag){
res.push_back(res1);
res.push_back(res2);
}
return res;
}
};