Two Sum

mail@pastecode.io avatar
unknown
c_cpp
17 days ago
485 B
4
Indexable
Never
class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        std::unordered_map<int,int> remainings;
        int size=nums.size();
        vector<int> solution;
        for(int i=0;i<size;i++)
        {
            int remaining=target-nums[i];
            if(remainings.contains(nums[i]))
            {
                return {remainings[nums[i]],i};
            }
            remainings[remaining]=i;
        }
        return {};
    }
};
Leave a Comment