c++ hash map solution


  • 0
    S
    class Solution {
    public:
        vector<int> twoSum(vector<int>& nums, int target) {
            vector<int> result;
            std::unordered_map<int, int > hashSimple;
            for(int i = 0; i< nums.size(); i++) {
                if(hashSimple.find(nums[i]) != hashSimple.end()) {
                     result.push_back(hashSimple[nums[i]]);
                     result.push_back(i);
                     return result;
                }
                else {
                    hashSimple[target-nums[i]] = i;
                }
            }
            return result;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.