vector<int> twoSum(vector<int> &numbers, int target)
{
//Key is the number and value is its index in the vector.
unordered_map<int, int> hash;
vector<int> result;
for (int i = 0; i < numbers.size(); i++) {
int numberToFind = target  numbers[i];
//if numberToFind is found in map, return them
if (hash.find(numberToFind) != hash.end()) {
//+1 because indices are NOT zero based
result.push_back(hash[numberToFind] + 1);
result.push_back(i + 1);
return result;
}
//number was not found. Put it in the map.
hash[numbers[i]] = i;
}
return result;
}
Accepted C++ O(n) Solution


Thanks for your post. However it would be better to share solution with correct code format and elaborated thoughts. Please read the Discuss FAQ for more info. Take a look at good sharing example

hash.find() takes constant time
http://www.cplusplus.com/reference/unordered_map/unordered_map/find/

Your answer seems O(n) but when I submitted your answer, the runtime was 20ms, which is worse than my answer of which the runtime is 16ms. My solution, However, is not O(n) but O(nlog(n)).
And I checked out the site:
http://www.cplusplus.com/reference/unordered_map/unordered_map/find/
which shows that the complexity of find() is linear in worst case.