Worst running time: O(n), Practical: 16ms.

Space: O(n).

```
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target)
{
unordered_map<int, int> numsMap;
vector<int> result;
for (int i = 0; i < nums.size(); ++i)
{
int addend = target - nums[i];
if (numsMap.find(addend) != numsMap.end())
{
result.push_back(numsMap[addend]+1);
result.push_back(i+1);
return result;
}
numsMap[nums[i]] = i;
}
}
};
```

Worst running time: O(nlogn), Practical: 12ms.

Space: O(n).

```
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> numsCopy(nums.begin(), nums.end());
sort(numsCopy.begin(), numsCopy.end()); // O(nlogn)
int i = 0;
int j = numsCopy.size()-1;
while (i < j)
{
int sum = numsCopy[i] + numsCopy[j];
if (sum < target)
i++;
else if (sum > target)
j--;
else
{
break;
}
}
return GetIndexOfAddends(nums, numsCopy[i], numsCopy[j]);
}
private:
vector<int> GetIndexOfAddends(const vector<int>& nums, int addend1, int addend2)
{
vector<int> result;
for (int i=0; i<nums.size(); i++)
{
if (nums[i] == addend1 || nums[i] == addend2)
{
result.push_back(i+1);
}
}
return result;
}
};
```