利用两数互补和哈希查找的原理
class Solution {
public:
/**
*
* @param numbers int整型vector
* @param target int整型
* @return int整型vector
*/
vector<int> twoSum(vector<int>& numbers, int target) {
std::vector<int> res;
std::unordered_map<int, int> hash;
for (int i = 0; i < numbers.size(); ++i) {
int tmp = target - numbers[i];
// 找不到
if (hash.find(tmp) == hash.end()) {
hash.insert({numbers[i], i});
} else {
res.push_back(hash[tmp] + 1);
res.push_back(i + 1);
break;
}
}
return res;
}
};