Untitled

mail@pastecode.io avatar
unknown
plain_text
23 days ago
790 B
2
Indexable
Never
class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target)
    {
        map<int,int> u_map;
        for(int i = 0; i < nums.size(); i++)
        {
            u_map[nums[i]] = (1+i);
        }
        int index = -1;
        for(auto x : u_map)
        {
            int value = target - x.first;
            if(value == x.first)
               {
                 index = x.second-1;
                 break;
               }
            if(u_map[value])
            return {x.second-1, u_map[value]-1};
        }
        if(index != -1)
        {
            for(int i =0; i < index; i++)
            {
                if(nums[i] == nums[index])
                return {i, index};
            }
        }
        return {-1, -1};
    }
};
Leave a Comment