剑指offer二十七:和为s的两个数字

剑指offer二十七:和为s的两个数字

 剑指offer二十七:和为s的两个数字

 

#include<iostream>
using namespace std;

bool findNumbersWithSum(int data[], int length, int sum)
{
	bool found = false;
	if (length < 1)
		return found;
	int ahead = 0;
	int behind = length - 1;

	while (ahead < behind)
	{
		int curSum = data[ahead] + data[behind];
		if (curSum == sum)
		{
			cout << data[ahead] << ' ' << data[behind] << endl;
			found = true;
			break;
		}
		else if (curSum > sum)
			behind--;
		else
			ahead++;
	}
	return found;
}

int main()
{
	int a[] = { 1,2,4,7,11,15 };
	bool found = findNumbersWithSum(a, 6, 15);
	cout << found << endl;
}

剑指offer二十七:和为s的两个数字