C++ : 0 ms solution


  • 0
    A
    class Solution {
    public:
        vector<int> twoSum(vector<int>& nums, int target);
    };
    
    vector<int> Solution::twoSum(vector<int>& nums, int target)
    {
        unordered_map<int, int> my_map;
        vector<int> ans;
        
        for(int i = 0; i < nums.size(); i++)
        {
            my_map.insert(pair<int, int>(nums[i], i));
        }
        
        for(int i = 0; i < nums.size(); i++)
        {
            auto it = my_map.find(target - nums[i]);
            
            if(it != my_map.end() && i != it->second)
            {
                ans.push_back(i);
                ans.push_back(it->second);
                break;
                
            }
        }
        
        return ans;
    }
    

Log in to reply
 

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