C++ logn add nlogn find beat 98%


  • 0
    M

    class TwoSum {
    public:
    /** Initialize your data structure here. */
    TwoSum() {

    }
    
    /** Add the number to an internal data structure.. */
    void add(int number) {
        nums.insert(number);
    }
    
    /** Find if there exists any pair of numbers which sum is equal to the value. */
    bool find(int value) {
        auto begin = nums.begin(), end = nums.end();
        if(begin == end)
            return false;
        --end;
    
        while(end != begin) {
            int sum = *begin + *end;
            if(sum == value)
                return true;
            else if(sum > value) {
                --end;
            }
            else
                ++begin;
        }
        return false;
    }
    
    multiset<int> nums;
    

    };


Log in to reply
 

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