Quang Văn

1 bundle
1 file17 days ago
1

Two Sum

1.1

class Solution
{
public:
    vector<int> twoSum(vector<int>& nums, int target)
    {
        unordered_map<int, size_t> N;
        vector<int> res;
        for (size_t i = 0; i < nums.size(); ++i) {
            if (N.find((const int)(target - nums[i])) != N.end()) {
                res.push_back(i);
                res.push_back(N[target - nums[i]]);
                break;
            }
            else {
                N.insert({nums[i], i });
            }
        }
        return res;
    }
};