My 16ms C++ solution


  • 0
    F

    Use unordered_map

    class Solution {
    public:
        vector<int> twoSum(vector<int>& nums, int target) {
            
            vector<int> result(2);
            unordered_map<int,int> intMap;
            for(int i=0; i<nums.size(); ++i){
                if(intMap.find(target-nums[i]) != intMap.end()){
                        result[0] = intMap[target-nums[i]] + 1;
                        result[1] = i + 1;
                        return result;
                }
                intMap[nums[i]] = i;
            }
        }
    };

Log in to reply
 

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