1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

#include <iostream>
#include <vector>
#include <map>

using namespace std;
vector<int> TwoSum(vector<int> &numbers, int target) 
{
	map<int,int> mem; //use hashtable to make run time become O(n)
	vector<int> indics;
	for (int i = 0; i < numbers.size(); ++i)
	{
		map<int,int>::iterator it = mem.find(numbers[i]);
		if (it != mem.end()) //number has match
		{
            indics.push_back((*it).second);
		    indics.push_back(i+1);
			break;
		}

		//save subtitution
		mem[target - numbers[i]] = i+1;
	}

	return indics;
}



int main(int argc, char** argv)
{
	
	return 0;
}
View Program Text


Test Status