面试金典之(一)找出和为SUM的递增数组array中的两个数使之乘积最小

class Solution {
public:
	vector FindNumbersWithSum(vector array, int sum) 
	{
		vectorres;
		int temp = 0;
		int i = 0, j = array.size() - 1;
		while (i < j&&temp!=sum)
		{
			temp = array[i] + array[j];
			if (temp>sum)j--;
			else if (temp == sum)
			{
				res.push_back(array[i]);
				res.push_back(array[j]);
				break;
			}
				else i++;
		}
		return res;
	}
};

你可能感兴趣的:(面试编程题库,数据结构与算法)