排序数组中和为给定值的两个数字
题目:输入一个已经按升序排序过的数组和一个数字,在数组中查找两个数,使得它们的和正好是输入的那个数字。要求时间复杂度是O(n)。如果有多对数字的和等于输入的数字,输出任意一对即可。
例如输入数组1、2、4、7、11、15和数字15。由于4+11=15,因此输出4和11。
分析:如果我们不考虑时间复杂度,最简单想法的莫过去先在数组中固定一个数字,再依次判断数组中剩下的n-1个数字与它的和是不是等于输入的数字。可惜这种思路需要的时间复杂度是O(n2)。
我们假设现在随便在数组中找到两个数。如果它们的和等于输入的数字,那太好了,我们找到了要找的两个数字;如果小于输入的数字呢?我们希望两个数字的和再大一点。由于数组已经排好序了,我们是不是可以把较小的数字的往后面移动一个数字?因为排在后面的数字要大一些,那么两个数字的和也要大一些,就有可能等于输入的数字了;同样,当两个数字的和大于输入的数字的时候,我们把较大的数字往前移动,因为排在数组前面的数字要小一些,它们的和就有可能等于输入的数字了。
我们把前面的思路整理一下:最初我们找到数组的第一个数字和最后一个数字。当两个数字的和大于输入的数字时,把较大的数字往前移动;当两个数字的和小于数字时,把较小的数字往后移动;当相等时,打完收工。这样扫描的顺序是从数组的两端向数组的中间扫描。
问题是这样的思路是不是正确的呢?这需要严格的数学证明。感兴趣的读者可以自行证明一下。
参考代码:
1 /////////////////////////////////////////////////////////////////////// 2 // Find two numbers with a sum in a sorted array 3 // Output: ture is found such two numbers, otherwise false 4 /////////////////////////////////////////////////////////////////////// 5 bool FindTwoNumbersWithSum 6 ( 7 int data[], // a sorted array 8 unsigned int length, // the length of the sorted array 9 int sum, // the sum 10 int& num1, // the first number, output 11 int& num2 // the second number, output 12 ) 13 { 14 15 bool found = false; 16 if(length < 1) 17 return found; 18 19 int ahead = length - 1; 20 int behind = 0; 21 22 while(ahead > behind) 23 { 24 long long curSum = data[ahead] + data[behind]; 25 26 // if the sum of two numbers is equal to the input 27 // we have found them 28 if(curSum == sum) 29 { 30 num1 = data[behind]; 31 num2 = data[ahead]; 32 found = true; 33 break; 34 } 35 // if the sum of two numbers is greater than the input 36 // decrease the greater number 37 else if(curSum > sum) 38 ahead --; 39 // if the sum of two numbers is less than the input 40 // increase the less number 41 else 42 behind ++; 43 } 44 45 return found; 46 }
扩展(1):输入一个数组,判断这个数组中是不是存在三个数字i, j, k,满足i+j+k等于0。在我的英文博客http://codercareer.blogspot.com/2011/10/no-09-numbers-with-given-sum.html里详细讨论了这个题目。
扩展(2):如果输入的数组是没有排序的,但知道里面数字的范围,其他条件不变,如何在O(n)时间里找到这两个数字?这个的基本思路是先用哈希表实现O(n)的排序(请参照本面试题系列的第57题),接下来的步骤都一样了。
以上转自何海涛博客
扩展(1)的参考地址需要FQ,在此一并转载
No. 09 - Numbers with a Given Sum
Step
|
Num1
|
Num2
|
Sum
|
Comparing with
s
|
Operation
|
1
|
1
|
15
|
16
|
Greater
|
Select the previous
number of Num2
|
2
|
1
|
11
|
12
|
Less
|
Select the next number of
Num1
|
3
|
2
|
11
|
13
|
Less
|
Select the next
number of Num1
|
4
|
4
|
11
|
15
|
Equal
|
|
1 bool FindNumbersWithSum(int data[], int length, int sum, 2 int* num1, int* num2) 3 { 4 bool found = false; 5 if(length < 1 || num1 == NULL || num2 == NULL) 6 return found; 7 8 9 int ahead = length - 1; 10 int behind = 0; 11 12 13 while(ahead > behind) 14 { 15 long long curSum = data[ahead] + data[behind]; 16 17 18 if(curSum == sum) 19 { 20 *num1 = data[behind]; 21 *num2 = data[ahead]; 22 found = true; 23 break; 24 } 25 else if(curSum > sum) 26 ahead --; 27 else 28 behind ++; 29 } 30 31 32 return found; 33 }
1 bool FindNumbersWithSum(int data[], int length, int sum, int excludeIndex) 2 { 3 bool found = false; 4 int ahead = length - 1; 5 int behind = 0; 6 7 8 while(ahead > behind) 9 { 10 if(ahead == excludeIndex) 11 ahead --; 12 if(behind == excludeIndex) 13 behind ++; 14 15 16 long long curSum = data[ahead] + data[behind]; 17 18 19 if(curSum == sum) 20 { 21 found = true; 22 break; 23 } 24 else if(curSum > sum) 25 ahead --; 26 else 27 behind ++; 28 } 29 30 31 return found; 32 }
1 bool HasThreeNumbersWithSum0(int data[], int length) 2 { 3 bool found = false; 4 if(data == NULL || length < 3) 5 return found; 6 7 8 std::sort(data, data + length - 1); 9 10 11 for(int i = 0; i < length; ++i) 12 { 13 int sum = -data[i]; 14 int num1, num2; 15 found = FindNumbersWithSum(data, length, sum, i); 16 17 18 if(found) 19 break; 20 } 21 22 23 return found; 24 }
The author Harry He owns all the rights of this post. If you are going to use part of or the whole of this ariticle in your blog or webpages, please add a reference to http://codercareer.blogspot.com/. If you are going to use it in your books, please contact me (zhedahht@gmail.com) . Thanks.