Use multimap to solve this probelm with 31ms .cpp


  • 0
        vector<int> result;
        multimap<int,int> hm;
        vector<int>::iterator ite;
        int i;
        for(i=1,ite = numbers.begin();ite!=numbers.end();ite++,i++)
        {
            hm.insert(pair<int,int>(*ite,i));
        }
        multimap<int,int>::iterator item1;
        for(item1=hm.begin();item1!=hm.end();item1=hm.begin())
        {
            int first = item1->first;
            int second = item1->second;
            hm.erase(item1);
            multimap<int,int>::iterator item2 = hm.find(target-first);
            if(item2!=hm.end())
            {
                result.push_back(second<item2->second?second:item2->second);
                result.push_back(second>item2->second?second:item2->second);
                return result;
            }
        }

Log in to reply
 

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