16ms C++ solution, easy to understand


  • 3
    M
    class Solution {
    public:
        vector<int> twoSum(vector<int>& nums, int target) {
            std::unordered_map<int,int> store;
            for(int i = 0; i < nums.size(); ++i)
            {
                auto itr = store.find(nums[i]);
                if(itr != store.end())
                    return std::vector<int> {itr->second+1,i+1}; 
                else
                    store[target-nums[i]] = i;
            }
        }
    };

Log in to reply
 

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