Given an array of integers, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution.
Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2
解析:
(1)一开始的基本想法,将temp = target-numbers[i],然后在剩余的元素中查找temp,这样的时间复杂度为O(n*n).
(2)后来对于算法进一步改进,首先将原来的数据进行排序,查询的时候利用二分查找的方法,时间复杂度为O(n*logn).
#include <iostream>
#include <vector>
using namespace std;
void printfArray(vector<int> &numbers)
{
for(int i=0;i<numbers.size();i++)
{
cout << numbers.at(i) << " " ;
}
cout << endl;
}
void heapAdjust(vector<int> &numbers,int root,int length,vector<int> &indices)
{
int leftSun = 2*root+1;
if(leftSun >= length)
return;
int rightSun = leftSun + 1;
if(rightSun >= length)
{
if(numbers.at(root) >= numbers.at(leftSun))
{
return;
}else{
int temp = numbers.at(root);
numbers.at(root) = numbers.at(leftSun);
numbers.at(leftSun) = temp;
int index = indices.at(root);
indices.at(root) = indices.at(leftSun);
indices.at(leftSun) = index;
heapAdjust(numbers,leftSun,length,indices);
}
}else{
if(numbers.at(rightSun) < numbers.at(leftSun))
{
if(numbers.at(root) >= numbers.at(leftSun))
{
return;
}else{
int temp = numbers.at(root);
numbers.at(root) = numbers.at(leftSun);
numbers.at(leftSun) = temp;
int index = indices.at(root);
indices.at(root) = indices.at(leftSun);
indices.at(leftSun) = index;
heapAdjust(numbers,leftSun,length,indices);
}
}else{
if(numbers.at(root) >= numbers.at(rightSun))
{
return;
}else{
int temp = numbers.at(root);
numbers.at(root) = numbers.at(rightSun);
numbers.at(rightSun) = temp;
int index = indices.at(root);
indices.at(root) = indices.at(rightSun);
indices.at(rightSun) = index;
heapAdjust(numbers,rightSun,length,indices);
}
}
}
}
void heapSort(vector<int> &numbers,vector<int> &indices)
{
int length = numbers.size();
for(int i=(length/2-1);i>=0;i--)
{
heapAdjust(numbers,i,length,indices);
}
for(int i=0;i<length-1;i++)
{
int temp = numbers.at(0);
numbers.at(0) = numbers.at(length - 1 - i );
numbers.at(length - 1 - i ) = temp;
int index = indices.at(0);
indices.at(0) = indices.at(length - 1 - i );
indices.at(length - 1 - i ) = index;
heapAdjust(numbers,0,length-i-1,indices);
}
}
int biSearch(vector<int> &numbers,int begin,int end,int target)
{
while(begin <= end)
{
int mid = (begin + end)/2;
if(numbers.at(mid) == target)
return mid;
if( numbers.at(mid) > target)
{
end = mid - 1;
}else{
begin = mid + 1;
}
}
return 0;
}
vector<int> twoSum(vector<int> &numbers, int target) {
int i,j;
vector<int> indices;
for(int i=0;i<numbers.size();i++)
{
indices.push_back(i+1);
}
heapSort(numbers,indices);
vector<int> res;
for(i=0;i<(numbers.size()-1);i++)
{
int temp = target - numbers.at(i);
int result = biSearch(numbers,i+1,numbers.size()-1,temp);
if(result != 0)
{
if(indices.at(i) < indices.at(result))
{
res.push_back(indices.at(i));
res.push_back(indices.at(result));
}else{
res.push_back(indices.at(result));
res.push_back(indices.at(i));
}
return res;
}
}
}
int main(void)
{
vector<int> numbers;
numbers.push_back(2);
numbers.push_back(1);
numbers.push_back(9);
numbers.push_back(4);
numbers.push_back(4);
numbers.push_back(56);
numbers.push_back(90);
numbers.push_back(3);
vector<int> res = twoSum(numbers, 8);
printfArray(res);
system("pause");
return 0;
}