I use map to solve it by C++.


  • 4
    J
        class Solution {
     public:
         vector<int> twoSum(vector<int>& nums, int target){
            vector<int> v;
            map<int,int> m;`enter code here`
            for(int i=0;i<nums.size();i++){
                if(m.find(nums[i])!=m.end()){
                    v.push_back(m[nums[i]]);
                    v.push_back(i+1);
                    return v;
                }else{
                    m[target - nums[i]] = i+1;
                }
            }
        }
    };

Log in to reply
 

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