数组:
(1)从两头往中间同步进行
(2)从中间向两头进行
(3)若需要三个数的操作,其中一个指向中间,两外两个分别指向两头
(4)若是无序数组,并且数组数字范围一定,则可以采用计数排序或者哈希表
================================================================================
本文转自:http://zhedahht.blog.163.com/blog/static/2541117420072143251809/
题目:输入一个已经按升序排序过的数组和一个数字,在数组中查找两个数,使得它们的和正好是输入的那个数字。要求时间复杂度是O(n)。如果有多对数字的和等于输入的数字,输出任意一对即可。
例如输入数组1、2、4、7、11、15和数字15。由于4+11=15,因此输出4和11。
分析:如果我们不考虑时间复杂度,最简单想法的莫过去先在数组中固定一个数字,再依次判断数组中剩下的n-1个数字与它的和是不是等于输入的数字。可惜这种思路需要的时间复杂度是O(n2)。
我们假设现在随便在数组中找到两个数。如果它们的和等于输入的数字,那太好了,我们找到了要找的两个数字;如果小于输入的数字呢?我们希望两个数字的和再大一点。由于数组已经排好序了,我们是不是可以把较小的数字的往后面移动一个数字?因为排在后面的数字要大一些,那么两个数字的和也要大一些,就有可能等于输入的数字了;同样,当两个数字的和大于输入的数字的时候,我们把较大的数字往前移动,因为排在数组前面的数字要小一些,它们的和就有可能等于输入的数字了。
我们把前面的思路整理一下:最初我们找到数组的第一个数字和最后一个数字。当两个数字的和大于输入的数字时,把较大的数字往前移动;当两个数字的和小于数字时,把较小的数字往后移动;当相等时,打完收工。这样扫描的顺序是从数组的两端向数组的中间扫描。
问题是这样的思路是不是正确的呢?这需要严格的数学证明。感兴趣的读者可以自行证明一下。
参考代码:
///
// Find two numbers with a sum in a sorted array
// Output: ture is found such two numbers, otherwise false
///
bool FindTwoNumbersWithSum
(
int data[], // a sorted array
unsigned int length, // the length of the sorted array
int sum, // the sum
int& num1, // the first number, output
int& num2 // the second number, output
)
{
bool found = false;
if(length < 1)
return found;
int ahead = length - 1;
int behind = 0;
while(ahead > behind)
{
long long curSum = data[ahead] + data[behind];
// if the sum of two numbers is equal to the input
// we have found them
if(curSum == sum)
{
num1 = data[behind];
num2 = data[ahead];
found = true;
break;
}
// if the sum of two numbers is greater than the input
// decrease the greater number
else if(curSum > sum)
ahead --;
// if the sum of two numbers is less than the input
// increase the less number
else
behind ++;
}
return found;
}
c++实现代码:
// Selection_sort.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <string>
#define LENGTH 10
bool FindTwoNumbersWithSum(int data[],unsigned int length,int sum);
using namespace std;
int main()
{
cout<<"Begin my road of Amazon,I'll be success!!!"<<endl;
int data[5] = {1,2,3,4,5};
int num1,num2;
FindTwoNumbersWithSum(data,5,10);
return 0;
}
bool FindTwoNumbersWithSum(int data[],unsigned int length,int sum)
{
int num1,num2;
bool found = false;
if(length < 1)
return found;
int ahead = length - 1;
int behind = 0;
while(ahead > behind)
{
long long curSum = data[ahead] + data[behind];
// if the sum of two numbers is equal to the input
// we have found them
if(curSum == sum)
{
num1 = data[behind];
num2 = data[ahead];
cout<<"num1: "<<num1<<endl;
cout<<"num2: "<<num2<<endl;
found = true;
break;
}
// if the sum of two numbers is greater than the input
// decrease the greater number
else if(curSum > sum)
ahead --;
// if the sum of two numbers is less than the input
// increase the less number
else
behind ++;
}
return found;
}
扩展(1):输入一个数组,判断这个数组中是不是存在三个数字i, j, k,满足i+j+k等于0。在我的英文博客http://codercareer.blogspot.com/2011/10/no-09-numbers-with-given-sum.html里详细讨论了这个题目。
网友代码:
扩展(1)的那个地址访问不了,我的做法不知道对否?
//主函数中调用findZeroSet(0,a.length-1)
void findZeroSet(int left, int right) {
if(left>=right)
return;
int j=(left+right)/2;
int i=left;
int k=right;
while(a[i]+a[k]+a[j]>0&&j>i)
j--;
if(j==i)
findZeroSet(left,right-1);
while(a[i]+a[k]+a[j]<0&&j<k)
j++;
if(j==k)
findZeroSet(left+1,right);
if(i!=j&&j!=k&&a[i]+a[k]+a[j]==0){
System.out.println(a[i]+" "+a[j]+" "+a[k]);
findZeroSet(left+1,right-1);//加这一句可找出所有的可能情况
}
}
扩展(2):如果输入的数组是没有排序的,但知道里面数字的范围,其他条件不变,如何在O(n)时间里找到这两个数字?这个的基本思路是先用哈希表实现O(n)的排序(请参照本面试题系列的第57题),接下来的步骤都一样了。