思路:
1. 暴力法 O(n2)
2. 哈希表,转换为找target - nums[i]的问题,查找用哈希表
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
vector<int> res;
unordered_map<int, int> hash;
for (int i = 0; i < nums.size(); i++) {
int other = target - nums[i];
if (hash.count(other)) {
res = vector<int> {hash[other], i};
break;
}
hash[nums[i]] = i;
}
return res;
}
};