My c++ solution beats 91%


  • 0
    X
    class TwoSum {
    public:
        /** Initialize your data structure here. */
        TwoSum() 
        {
            
        }
        
        /** Add the number to an internal data structure.. */
        void add(int number)
        {
            data[number]++;
        }
        
        /** Find if there exists any pair of numbers which sum is equal to the value. */
        bool find(int value)
        {
            for(auto it:data)
                if(data.count(value-it.first)&&(data[value-it.first]>1||value-it.first!=it.first))
                    return true;
            return false;
        }
        
        map<int,int> data;
    };
    

Log in to reply
 

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