Solution 1:

using unordered_map: 19ms beat 62.52%.

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

Solution 2:

But using sort()+two pointer: 9ms beat 96.91

```
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> bak = nums;
sort(nums.begin(), nums.end());
vector<int> res;
int i = 0, j = nums.size() - 1;
while (i < j) {
int sum = nums[i] + nums[j];
if (sum == target) {
for (int k = 0; k < bak.size(); k++) {
if (bak[k] == nums[i] || bak[k] == nums[j]) {
res.push_back(k);
}
}
return res;
} else if (sum < target) {
i++;
} else {
j--;
}
}
return res;
}
};
```

Anybody tell me why? I used to believe Solution1 consume less time...But ..