class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> hash;//无须有序
for(int i=0; i<nums.size(); i++){
auto it = hash.find(target-nums[i]);//找对应数
if(it != hash.end()){
return {i, it->second};
}
// 如果没找到,将当前遍历元素存入哈希表
hash[nums[i]] = i;
}
return {};
}
};
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
for(int i=0; i<nums.size(); i++){
for(int j=i+1; j<nums.size(); j++){
if(nums[i] + nums[j] == target){
return {i, j};
}
}
}
return {};
}
};