Description

LINK: https://leetcode.com/problems/two-sum

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

1
2
3
4
Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
static auto x = [](){
// turn off sync
std::ios::sync_with_stdio(false);
// untie in/out streams
cin.tie(nullptr);
return 0;
}();

class Solution {
private:
unordered_map<int, int> umap;
public:
vector<int> twoSum(vector<int>& nums, int target) {
int size = nums.size();
for (int i = 0; i < size; i++){
auto r = umap.find(target - nums[i]);
if (r != umap.end()){
return vector<int>{i, r->second};
}
umap[nums[i]] = i;
}
}
};

Discussion

前面那一段,用来解决频繁输出到stdout中很费时间的问题。

后面,用unordered_map实现快速查找,很神奇。

另外,把unordered_map<int, int> umap;的初始化放到函数外面,进一步提速(很trick)。