13 lines c++ code using map


  • 6
    W
    class Solution {
    public:
        vector<int> twoSum(vector<int>& nums, int target) {
            map<int,int> map1;
            for(int i = 0; i < nums.size(); i++) {
                if(map1.find(target-nums[i]) != map1.end()) {
                    vector<int> a = { map1[target-nums[i]], i+1};
                    return a;
                }
                map1[nums[i]] = i+1;
            }
        }
    };

Log in to reply
 

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