c++ method with map


  • 0
    X
    vector<int> twoSum(vector<int>& numbers, int target) {
          std::vector<int> indice;
          std::unordered_map<int,int> resid;
          for(int i=0;i<numbers.size();i++){        
            std::map<int,int>::iterator it=resid.find(numbers[i]);
            if(it==resid.end())
              resid[target-numbers[i]]=i;
            else{
                indice.push_back(it->second+1); 
                indice.push_back(i+1);
            }
          }
          return indice;
        }
    

Log in to reply
 

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