16ms with unordered_map


  • 1
    F

    class Solution {
    public:
    vector<int> twoSum(vector<int>& nums, int target) {
    unordered_map<int, int> um;
    int len=nums.size();
    vector<int> v;
    for(int i=0; i<len; ++i)
    {
    if(um.find(nums[i])==um.end())
    {
    int diff=target-nums[i];
    um[diff]=i;
    }
    else
    {
    v.push_back(um[nums[i]]+1);
    v.push_back(i+1);
    return v;
    }
    }
    return v;
    }
    };


Log in to reply
 

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