description:

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:

Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

my answer:

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        unordered_map<int, int>m;
        for(int i = 0; i < nums.size(); i++){
            if(m.count(target - nums[i])){
                return {m[target - nums[i]], i};
                break;
            }
            m[nums[i]] = i;
        }
        return {};
    }
};

用哈希表找它对应的元素是否在map里

relative point get√:

hint :

关于为什么用nums[i]作key,不会有重复吗?
重复并不影响,比方说target=14,array = [7,1,7]
对于第一个7: 14 - 7 = 7 不在m里,加入 m
对于第二个7: 14 - 7 = 7 在m里,所以直接返回{2,1}
对于重复的数字和另一个数字组成target或者答案不涉及重复数字的情况:
也不影响,重复数字在答案里,会与假设相悖。不涉及的话就直接覆盖也不影响。