输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的。
class Solution {
public:
vector<int> FindNumbersWithSum(vector<int> array,int sum) {
int length = array.size();
vector<int> ivec;
int min = 0;
for (int i = 0;i!=length; i++)
{
for (int j = 0;j!=length;j++)
{
if(j==i)
continue;
if (array[j] == sum-array[i])
{
if (min == 0)
{
min = array[i] * array[j];
ivec.push_back(array[i]);
ivec.push_back(array[j]);
}
if(min > array[i]*array[j])
{
ivec.clear();
min = array[i] * array[j];
ivec.push_back(array[i]);
ivec.push_back(array[j]);
}
}
}
}
sort(ivec.begin(),ivec.end());
return ivec;
}
};