Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
791 B
4
Indexable
Never
class TwoSum {
public:
    TwoSum() {}

    void add(int number) {
        arr_.push_back(number);
        if (map_.count(number)) {
            map_[number]++;
        } else {
            map_[number] = 1;
        }
    }

    bool find(int value) {
        for (long i : arr_) {
            if (map_.count(value - i)) {
                if (value - i != i ) {
                    return true;
                }
                if (map_[i] > 1) {
                    return true;
                }
            }
        }
        return false;
    }

private:
    std::vector<long> arr_;
    std::unordered_map<long, int> map_;
};

/**
 * Your TwoSum object will be instantiated and called as such:
 * TwoSum* obj = new TwoSum();
 * obj->add(number);
 * bool param_2 = obj->find(value);
 */
Leave a Comment